看了前面一節: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 不支持標準輸入,因此數據被丟棄
新聞熱點
疑難解答