linux shell 管道命令(pipe)使用及與shell重定向區別

看了前面一節:linux shell數據重定向(輸入重定向與輸出重定向)詳細分析 估計還有一些朋友是頭暈暈的,好複雜的重定向了。此次咱們看下管道命令了。shell管道,能夠說用法就簡單多了。html

 

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

 

  • 管道命令使用說明:

先看下下面圖:linux

image

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

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

注意:bash

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

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

實例:測試

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
[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 等等,都是些文本處理命令。url

  • 管道命令與重定向區別

區別是:

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

 

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

這些都是網上總結不少的,其實只要多加清楚用法,也必定有本身的一份不一樣描述。

實例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#能夠相互轉換狀況
#輸入重定向
 
[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,經過管道繼續往下面傳遞數據爲空,因此沒有什麼顯示的
 
#一樣">"輸出重定向符,優先級也是先解析,當一個命令有這個字符,它就會與左邊命令標準輸出綁定。準備好了這些,就等待命令執行輸出數據,它就開始接收

 

再歸納下:

從上面例子能夠看,重定向與管道在使用時候不少時候能夠通用,其實,在shell裏面,常常是【條條大路通羅馬】的。通常若是是命令間傳遞參數,仍是管道的好,若是處理輸出結果須要重定向到文件,仍是用重定向輸出比較好。

命令執行順序能夠看下:Linux Shell 通配符、元字符、轉義符使用實例介紹

 

 

  • shell腳本接收管道輸入

有意思的問題:

既然做用管道接收命令,須要能夠接收標準的輸入,那麼咱們shell腳本是否能夠開發出這樣的基本程序呢?(你們常常看到的,都是一些系統的命令做爲管道接收方)

實例(testpipe.sh):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/bin/sh
  
  if  [ $ # -gt 0 ];then
      exec  0<$1;
#判斷是否傳入參數:文件名,若是傳入,將該文件綁定到標準輸入
  fi
  
  while  read  line
  do
      echo  $line;
  done <&0;
#經過標準輸入循環讀取內容
  exec  0&-;
#解除標準輸入綁定

運行結果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[chengmo@centos5 shell]$ cat  testpipe.txt
1,t,est pipe
2,t,est pipe
3,t,est pipe
4,t,est pipe
#testpipe.txt 只是須要讀取的測試文本
 
[chengmo@centos5 shell]$ cat  testpipe.txt | sh testpipe.sh
1,t,est pipe
2,t,est pipe
3,t,est pipe
4,t,est pipe
#經過cat 讀取 testpipe.txt 發送給testpipe.sh 標準輸入
 
[chengmo@centos5 shell]$ sh testpipe.sh testpipe.txt     
1,t,est pipe
2,t,est pipe
3,t,est pipe
4,t,est pipe
#testpipe.sh 經過出入文件名讀取文件內容
分類:  linux



相關文章
相關標籤/搜索