Linux管道指令(pipe)與shell 重定向的區別

多指令執行python

符號
格式 做用
; comd1;comd2  依次執行
&& comd1 && comd2

comd1正確執行,commd2執行,shell

comm1執行失敗, commd2不執行centos

|| comd1 || comd2 

 comm1執行失敗,執行commd1less

comm1執行成功,不執行commd2spa

管道 (|)與重定向(>, >>)的區別

管道命令操做符是:」|」,它僅能處理經由前面一個指令傳出的正確輸出信息,也就是 standard output 的信息,對於 stdandard error 信息沒有直接處理能力。而後,傳遞給下一個命令,做爲標準的輸入 standard input.code

command1正確輸出,做爲command2的輸入 而後comand2的輸出做爲,comand3的輸入 ,comand3輸出就會直接顯示在屏幕上面了。orm

經過管道以後:comand1,comand2的正確輸出不顯示在屏幕上面進程

注意:ip

一、管道命令只處理前一個命令正確輸出,不處理錯誤輸出input

二、管道命令右邊命令,必須可以接收標準輸入流命令才行。

實例:

[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 | ls
catfile      httprequest.txt  secure  test            testfdread.sh  testpipe.sh    testsh.sh      testwhile2.sh
envcron.txt  python           sh      testcase.sh     testfor2.sh    testselect.sh  test.txt       text.txt
env.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 等等,都是些文本處理命令。

  • 管道命令與重定向區別

區別是:

一、左邊的命令應該有標準輸出 | 右邊的命令應該接受標準輸入
    左邊的命令應該有標準輸出 > 右邊只能是文件
    左邊的命令應該須要標準輸入 < 右邊只能是文件

二、管道觸發兩個子進程執行"|"兩邊的程序;而定向是在一個進程內執行

[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.sh
10: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.sh
testsh.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,經過管道繼續往下面傳遞數據爲空,因此沒有什麼顯示的
#一樣">"輸出重定向符,優先級也是先解析,當一個命令有這個字符,它就會與左邊命令標準輸出綁定。準備好了這些,就等待命令執行輸出數據,它就開始接收
相關文章
相關標籤/搜索