聲明:本文僅作學習研究使用,多數語句都是為了介紹文法而構造的。
重定向輸入、輸出示例
$cat #cat把鍵盤看作标準輸入,螢幕看作标準輸出。按下ctrl+d結束鍵盤輸入
$cat > sample.txt
$cat /dev/null > /var/log/messages
$cat /etc/profile > /var/log/messages
$cat /etc/profile >> /var/log/messages #在檔案/var/log/messages末尾追加/etc/profile 的内容
$cat /etc/profile /home/shell.txt > /var/log/messages
$cat /etc/profile /home/shell.txt 1 > hold1 2 > hold2 #将标準輸出定向到hold1中,将标準錯誤輸出定向到hold2中
$exec 1> fd1.out #将以後所有指令的輸出都定向到fd1.out
$ln -s ch05.doc ./docs >> /tmp/ln.log 2>/dev/null #将連接配接檔案的資訊追加到/tmp/ln.log中,并将錯誤輸出定向到/dev/null中
$rm -rf /tmp/my_tmp_dir > /dev/null 2>&1 #将标準錯誤輸出和标準輸出都定向到/dev/null中
$who | tee file.a | wc -l #重定向到管道傳遞給tee指令後繼續将結果傳遞給wc指令
$cat /etc/profile /home/shell.txt | tr "[a-z]" "[a-z]"
$who | sort
$ls | less
将循環的輸出重新排序
#!/bin/bash
#filename:output_sort.sh
#datetime:2010_12_24 15:56
#discription:sort the output number
for i in 7 9 2 4 5 12
do
echo $i
done | sort -n //将變量$i中的數值進行排序
exit 0
輸入重定向(利用read讀入檔案/etc/fstab的前兩行)
#filename:twolines_fstab
#datetime:2010_12_24 15:59
#discription:output the two lines of fstab
file=/etc/fstab
{
read line1 //讀入第一行
read line2 //讀入第二行
} < $file
echo "first line in $file is:\"$line1\"" //輸出第一行結果
echo "second line in $file is:\"$line2\"" //輸出第二行結果
每5分鐘将将登入進入系統的使用者清單追加到logfile檔案中
#filename:record_loginuser.sh
#datetime:2010_12_24 16:16
#discription:record the username who login system every 5 minutes
while : //無限循環開始
date
who
sleep 300 //睡眠5分鐘
done >> logfile //将記錄的結果重定向到logfile檔案中
原創文章,轉載請注明出處、作者
如有錯誤,歡迎指正
作者:czmmiao 原文位址:http://czmmiao.iteye.com/blog/911372