看了前面一節:linux shell數據重定向(輸入重定向與輸出重定向)詳細分析 估計還有一些朋友是頭暈暈的,好復雜的重定向了。這次我們看下管道命令了。shell管道,可以說用法就簡單多了。
管道命令操作符是:”|”,它僅能處理經由前面一個指令傳出的正確輸出信息,也就是 standard output 的信息,對于 stdandard
error 信息沒有直接處理能力。然后,傳遞給下一個命令,作為標準的輸入 standard input.
管道命令使用說明:
先看下下面圖:
command1正確輸出,作為command2的輸入 然后comand2的輸出作為,comand3的輸入 ,comand3輸出就會直接顯示在屏幕上面了。
通過管道之后:comand1,comand2的正確輸出不顯示在屏幕上面
注意:
1、管道命令只處理前一個命令正確輸出,不處理錯誤輸出
2、管道命令右邊命令,必須能夠接收標準輸入流命令才行。
實例:
[chengmo@centos5 shell]$ cat test.sh | grep -n 'echo'5: echo "very good!";7: echo "good!";9: echo "pass!";11: echo "no pass!";#讀出test.sh文件內容,通過管道轉發給grep 作為輸入內容 [chengmo@centos5 shell]$ cat test.sh test1.sh | grep -n 'echo'cat: test1.sh: 沒有那個文件或目錄5: echo "very good!";7: echo "good!";9: echo "pass!";11: echo "no pass!";#cat test1.sh不存在,錯誤輸出打印到屏幕,正確輸出通過管道發送給grep [chengmo@centos5 shell]$ cat test.sh test1.sh 2>/dev/null | grep -n 'echo' 5: echo "very good!";7: echo "good!";9: echo "pass!";11: echo "no pass!";#將test1.sh 沒有找到錯誤輸出重定向輸出給/dev/null 文件,正確輸出通過管道發送給grep [chengmo@centos5 shell]$ cat test.sh | lscatfile httprequest.txt secure test testfdread.sh testpipe.sh testsh.sh testwhile2.shenvcron.txt python sh testcase.sh testfor2.sh testselect.sh test.txt text.txtenv.txt release sms testcronenv.sh testfor.sh test.sh testwhile1.sh#讀取test.sh內容,通過管道發送給ls命令,由于ls 不支持標準輸入,因此數據被丟棄
這里實例就是對上面2點注意的驗證。作用接收標準輸入的命令才可以用作管道右邊。否則傳遞過程中數據會拋棄。 常用來作為接收數據管道命令有:sed,awk,cut,head,top,less,more,wc,join,sort,split 等等,都是些文本處理命令。
管道命令與重定向區別
區別是:
1、左邊的命令應該有標準輸出 | 右邊的命令應該接受標準輸入
左邊的命令應該有標準輸出 > 右邊只能是文件
左邊的命令應該需要標準輸入 < 右邊只能是文件
2、管道觸發兩個子進程執行"|"兩邊的程序;而重定向是在一個進程內執行
這些都是網上總結很多的,其實只要多加清楚用法,也一定有自己的一份不同描述。
實例:
#可以相互轉換情況#輸入重定向 [chengmo@centos5 shell]$ cat test.sh| grep -n 'echo'5: echo "very good!";7: echo "good!";9: echo "pass!";11: echo "no pass!";#"|"管道兩邊都必須是shell命令 [chengmo@centos5 shell]$ grep -n 'echo' <test.sh 5: echo "very good!";7: echo "good!";9: echo "pass!";11: echo "no pass!";#"重定向"符號,右邊只能是文件(普通文件,文件描述符,文件設備) [chengmo@centos5 shell]$ mail -s 'test' 8292669@qq.com <test.sh[chengmo@centos5 shell]$ cat test.sh|mail -s 'test' 8292669@qq.com#以上2個也相同,將test.sh內容發送到指定郵箱。 [chengmo@centos5 shell]$ (sed -n '1,$p'|grep -n 'echo')<test.sh 5: echo "very good!";7: echo "good!";9: echo "pass!";11: echo "no pass!";#這個腳本比較有意思了。由于前面是管道,后面需要把test.sh內容重定向到 sed ,然后sed輸出通過管道,輸入給grep.需要將前面用"()"運算符括起來。在單括號內的命令,可以把它們看作一個象一個命令樣。如果不加括號test.sh就是grep 的輸入了。 #上面一個等同于這個[chengmo@centos5 shell]$ sed -n '1,$p'<test.sh | grep -n 'echo'5: echo "very good!";7: echo "good!";9: echo "pass!";11: echo "no pass!"; #重定向運算符,在shell命令解析前,首先檢查的(一個命令,執行前一定檢查好它的輸入,輸出,也就是0,1,2 設備是否準備好),所以優先級會最高 [chengmo@centos5 shell]$ sed -n '1,10p'<test.sh | grep -n 'echo' <testsh.sh10:echo $total;18:echo $total;21: echo "ok";#哈哈,這個grep又接受管道輸入,又有testsh.sh輸入,那是不是2個都接收呢。剛才說了"<"運算符會優先,管道還沒有發送數據前,grep綁定了testsh.sh輸入,這樣sed命令輸出就被拋棄了。這里一定要小心使用 #輸出重定向 [chengmo@centos5 shell]$ cat test.sh>test.txt[chengmo@centos5 shell] cat test.sh|tee test.txt &>/dev/null#通過管道實現將結果存入文件,還需要借助命令tee,它會把管道過來標準輸入寫入文件test.txt ,然后將標準輸入復制到標準輸出(stdout),所以重定向到/dev/null 不顯示輸出#">"輸出重定向,往往在命令最右邊,接收左邊命令的,輸出結果,重定向到指定文件。也可以用到命令中間。 [chengmo@centos5 shell]$ ls test.sh test1.sh testsh.sh 2>err.txt | grep 'test'test.shtestsh.sh#目錄下面有:test,testsh文件,test1.sh不存在,因此將ls 命令錯誤輸出輸入到err.txt 正確輸出,還會通過管道發送到grep命令。[chengmo@centos5 shell]$ ls test.sh test1.sh testsh.sh &>err.txt | grep 'test'#這次打印結果是空,&代表正確與錯誤輸出 都輸入給err.txt,通過管道繼續往下面傳遞數據為空,所以沒有什么顯示的 #同樣">"輸出重定向符,優先級也是先解析,當一個命令有這個字符,它就會與左邊命令標準輸出綁定。準備好了這些,就等待命令執行輸出數據,它就開始接收
再概括下:
從上面例子可以看,重定向與管道在使用時候很多時候可以通用,其實,在shell里面,經常是【條條大路通羅馬】的。一般如果是命令間傳遞參數,還是管道的好,如果處理輸出結果需要重定向到文件,還是用重定向輸出比較好。
命令執行順序可以看下:Linux Shell 通配符、元字符、轉義符使用實例介紹
shell腳本接收管道輸入
有意思的問題:
既然作用管道接收命令,需要可以接收標準的輸入,那么我們shell腳本是否可以開發出這樣的基本程序呢?(大家經??吹降模际且恍┫到y的命令作為管道接收方)
實例(testpipe.sh):
#!/bin/sh if [ $# -gt 0 ];then exec 0<$1;#判斷是否傳入參數:文件名,如果傳入,將該文件綁定到標準輸入 fi while read line do echo $line; done<&0;#通過標準輸入循環讀取內容 exec 0&-;#解除標準輸入綁定
運行結果:
[chengmo@centos5 shell]$ cat testpipe.txt1,t,est pipe2,t,est pipe3,t,est pipe4,t,est pipe#testpipe.txt 只是需要讀取的測試文本 [chengmo@centos5 shell]$ cat testpipe.txt | sh testpipe.sh1,t,est pipe2,t,est pipe3,t,est pipe4,t,est pipe#通過cat 讀取 testpipe.txt 發送給testpipe.sh 標準輸入 [chengmo@centos5 shell]$ sh testpipe.sh testpipe.txt 1,t,est pipe2,t,est pipe3,t,est pipe4,t,est pipe#testpipe.sh 通過出入文件名讀取文件內容
新聞熱點
疑難解答