http://mblog.sigma.me/2011/08/15/linux-output-redirect.html
Linux shell中有三種輸入輸出,分別為標准輸入,標准輸出,錯誤輸出,分別對應0,1,2。我們可以直接通過輸出重定向>(或>>,表示追加)將某種輸出重定向到其他地方,如設備,文件,比如:
1 |
ls > ls .log #標准輸出重定向 |
2 |
ls 2> ls .log #標准錯誤重定向 |
3 |
ls > /dev/null #重定向到null設備,相當於直接忽略輸出 |
但是,有時候,我們想把標准輸出以及錯誤輸出一起重定向某個文件,這是可以通過 2>&1 實現,也可以通過兩個同時重定向到某個文件
1 |
ls > ls .log 2>&1 //標准輸出重定向到 ls .log,而錯誤又重定向到標准輸出,這兩個位置不可換 |
2 |
ls 2>> ls .log 1>> ls .log |
http://hi.baidu.com/zhaolegend/blog/item/245ad226e860bdfed7cae2ed.html
先說一下linux重定向:
0、1和2分別表示標准輸入、標准輸出和標准錯誤信息輸出,可以用來指定需要重定向的標准輸入或輸出。 在一般使用時,默認的是標准輸出,既1.當我們需要特殊用途時,可以使用其他標號。例如,將某個程序的錯誤信息輸出到log文件中:./program 2>log。這樣標准輸出還是在屏幕上,但是錯誤信息會輸出到log文件中。 另外,也可以實現0,1,2之間的重定向。2>&1:將錯誤信息重定向到標准輸出。 Linux下還有一個特殊的文件 /dev/null,它就像一個無底洞,所有重定向到它的信息都會消失得無影無蹤。這一點非常有用,當我們不需要回顯程序的所有信息時,就可以將輸出重定向到/dev/null。 如果想要正常輸出和錯誤信息都不顯示,則要把標准輸出和標准錯誤都重定向到/dev/null, 例如: # ls 1>/dev/null 2>/dev/null 還有一種做法是將錯誤重定向到標准輸出,然后再重定向到 /dev/null,例如: # ls >/dev/null 2>&1 注意:此處的順序不能更改,否則達不到想要的效果,此時先將標准輸出重定向到 /dev/null,然后將標准錯誤重定向到標准輸出,由於標准輸出已經重定向到了/dev/null,因此標准錯誤也會重定向到/dev/null,於是一切靜悄悄:-)
由於使用nohup時,會自動將輸出寫入nohup.out文件中,如果文件很大的話,nohup.out就會不停的增大,這是我們不希望看到的,因此,可以利用/dev/null來解決這個問題。 nohup ./program >/dev/null 2>log & 如果錯誤信息也不想要的話: nohup ./program >/dev/null 2>&1 & |
http://czmmiao.iteye.com/blog/911372
聲明:本文僅作學習研究使用,多數語句都是為了介紹語法而構造的。
重定向輸入、輸出示例
$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的前兩行)
#!/bin/bash
#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\"" //輸出第二行結果
exit 0
每5分鍾將將登錄進入系統的用戶列表追加到logfile文件中
#!/bin/bash
#Filename:record_loginuser.sh
#Datetime:2010_12_24 16:16
#Discription:Record the username who login system every 5 minutes
while : //無限循環開始
do
date
who
sleep 300 //睡眠5分鍾
done >> logfile //將記錄的結果重定向到logfile文件中
參考至:http://club.topsage.com/viewthread.php?tid=668357&highlight=shell
原創文章,轉載請注明出處、作者
如有錯誤,歡迎指正
郵箱:czmcj@163.com
http://blog.csdn.net/flowingflying/article/details/5201199
本文也即《Learning the bash Shell》3rd Edition的第七章Input/Output and Command-Line Processing之讀書筆記之一。我們曾經學習過shell的基本IO重定向操作:>、<和|。基本上能滿足95%的情況,但是我們需要知道bash支持的重定向操作。
- cmd1 |cmd2 : pipe,將cmd1 的標准輸出作為cmd2 的標准輸入
- >file :將標准輸出重定向到file
- <file :將file作為標准輸入
- >>file :將標准輸出重定向到file,如果file存在,append到文件中,即附加到文件的后面,而不是覆蓋文件
當cat不帶參數的時候,表示使用標准輸入作為輸入,這允許在標准輸入中鍵入相關的內容,下面將alias加入.bashrc作為最后一行
$ cat >> .bashrc
alias cdmnt='mount -t iso9660 /dev/sbpcd /cdrom'
^D - >|file :強制將標准輸出重定向到file,即使noclobber設置。當設置環境變量set –o noclobber,將禁止重定向到一個已經存在的文件中,避免文件被覆蓋。
- n >|file :強制將文件描述符n重定向到file,即使noclobber打開
- <>file :將file作為標准輸入和標准輸出。通常用於設備文件(/dev下面的文件),底層系統程序員可用之測試設備驅動,其他的很少用。
- n <>file :將file作為文件描述符n的輸入和輸出
- <<label :Here-document; see text 。將shell的標准輸入作為命令的輸入,直到行中之包含label。這中間的輸入成為here-document。下面是一個例子。我們讓人使用cat >> file的方式,通過標准輸入在文件中附加內容。
$ cat >> msgfile << . #這里<<.表明以.為結束。因此無需使用^D,而改用.
> this is the text of
> our message.
> . #這里表示結束。則msgfile中增加了兩行this is…和our message.
MACHINE="i586"
OS="linux-gnu"
CC="gcc"
cat > $file <
Machine: $MACHINE
OS: $OS
Compiler: $CC
EOF
查看:cat $file,這里給出正常結構
Machine: i586
OS: linux-gnu
Compiler: gcc
如果在EOF加上單引號,或者雙引號如下
cat > $file <<'EOF'
則不解析$CC的內容,文件內容如下
Machine: $MACHINE
OS: $OS
Compiler: $CC
如果使用<<-,如下,則刪除所有行中前面打頭的> tab<這樣在腳本的書寫上會比較適合閱讀 ,例如上面的例子可以寫為:
cat > $file <<-EOF
Machine: $MACHINE
OS: $OS
Compiler: $CC
EOF - n>file :將文件描述符n重定向到file
- n :將file作為文件描述符的輸入
- n>>file :將文件描述符n的輸出重定向到file,如果file存在,將輸出append到文件后面
- n>& :將標准輸出復制到文件描述符n(Duplicate standard output to file descriptor n)
- n<& :從文件描述符n復制標准輸入(Duplicate standard input from file descriptor n)
- n>&m :文件描述字n將一個copy至文件描述字m(File descriptor n is made to be a copy of the output file descriptor)
- n<&m :文件描述字n作為文件描述字m中的一個拷貝(File descriptor n is made to be a copy of the input file descriptor)
- &>file : 將標准輸出和標准錯誤輸出定向至文件file
- <&- : 關閉標准輸入
- >&- : 關閉標准輸出
- n>&- : 關閉文件描述字作為輸出(Close the output from file descriptor n)
- n<&- :關閉文件描述字作輸入(Close the input from file descriptor n)
- n>&word: If n is not specified, the standard output (file descriptor 1) is used. If the digits in word do not specify a file descriptor open for output, a redirection error occurs. As a special case, if n is omitted, and word does not expand to one or more digits, the standard output and standard error are redirected as described previously.
- n<&word : If word expands to one or more digits, the file descriptor denoted by n is made to be a copy of that file descriptor. If the digits in word do not specify a file descriptor open for input, a redirection error occurs. If word evaluates to -, file descriptor n is closed. If n is not specified, the standard input (file descriptor 0) is used.
- n>&digit- : Moves the file descriptor digit to file descriptor n, or the standard output (file descriptor 1) if n is not specified.
- n<&digit- : Moves the file descriptor digit to file descriptor n, or the standard input (file descriptor 0) if n is not specified. digit is closed after being duplicated to n.
文件描述符
文件描述符在bash中比較少用,從0開始用戶表示進行的數據流,0表示標准輸入,1表示標准輸出,2表示標注錯誤輸出,其他從3開始。最為常用的場景是將錯誤消息輸出到某個文件,可以加上2>file 到我們的命令中。
我們來看下面一個腳本的例子:
command > logfile 2>&1 &
>logfile,表示command的標准輸出重定向至文件logfile中,2>&1,匹配n>&m,表示文件描述字2(command的標准錯誤輸出)將copy一份采用文件描述字1(即標准輸出),由於標准輸出已經重定向logfile,這份copy也見將重定向至文件lofgile。我們可以用“abcd > logfile 2>&1 &”來驗證這個效果。最后&表示后台運行的方式。這樣命令表示在后台運行command,而它的標准輸出和錯誤輸出均重定向到logfile文件中。下面可達到類似的效果:
command 2>&1 | tee logfile &
錯誤輸出同樣適用標准輸出,通過pipe方式,見他們作為輸入執行tee logfile。tee命令將它的標准輸入copy至他的標准標准輸出以及參數所帶的文件中。和上面的命令不一眼這里即會在stdout 和logfile中同時輸出。
其他文件描述字的重定向,例如<&n,通常用於從多個文件中讀入或者寫出。
<&- ,表示強制關閉標准輸入
>&- ,表示強制關閉標准輸出
1> ,等同於>
0< ,等同於<