Linux Shell 重定向與管道【轉帖】


by 程默

在了解重定向之前,我們先來看看linux 的文件描述符。

linux文件描述符:可以理解為linux跟蹤打開文件,而分配的一個數字,這個數字有點類似c語言操作文件時候的句柄,通過句柄就可以實現文件的讀寫操作。 用戶可以自定義文件描述符范圍是:3-num,這個最大數字,跟用戶的:ulimit –n 定義數字有關系,不能超過最大值。

 

linux啟動后,會默認打開3個文件描述符,分別是:標准輸入standard input 0,正確輸出standard output 1,錯誤輸出:error output 2

以后打開文件后。新增文件綁定描述符 可以依次增加。 一條shell命令執行,都會繼承父進程的文件描述符。因此,所有運行的shell命令,都會有默認3個文件描述符。

 

對於任何一條linux 命令執行,它會是這樣一個過程:

image

一個命令執行了:

先有一個輸入:輸入可以從鍵盤,也可以從文件得到

命令執行完成:成功了,會把成功結果輸出到屏幕:standard output默認是屏幕

命令執行有錯誤:會把錯誤也輸出到屏幕上面:standard error默認也是指的屏幕

 

文件輸入輸出由追蹤為一個給定的進程所有打開文件的整數句柄來完成。這些數字值就是文件描述符。最為人們所知的文件米描述符是 stdinstdout 和 stderr,文件描述符的數字分別是0,1和2。這些數字和各自的設備是保留的。一個命令執行前,先會准備好所有輸入輸出,默認分別綁定(stdin,stdout,stderr),如果這個時候出現錯誤,命令將終止,不會執行。命令解析過程,可以參考:Linux Shell 通配符、元字符、轉義符使用實例介紹

 

這些默認的輸出,輸入都是linux系統內定的,我們在使用過程中,有時候並不希望執行結果輸出到屏幕。我想輸出到文件或其它設備。這個時候我們就需要進行輸出重定向了。

 

linux shell下常用輸入輸出操作符是:

1.  標准輸入   (stdin) :代碼為 0 ,使用 < 或 << ; /dev/stdin -> /proc/self/fd/0   0代表:/dev/stdin 
2.  標准輸出   (stdout):代碼為 1 ,使用 > 或 >> ; /dev/stdout -> /proc/self/fd/1  1代表:/dev/stdout
3.  標准錯誤輸出(stderr):代碼為 2 ,使用 2> 或 2>> ; /dev/stderr -> /proc/self/fd/2 2代表:/dev/stderr

 

  • 輸出重定向:

格式:

command-line1 [1-n] > file或文件操作符或設備

上面命令意思是:將一條命令執行結果(標准輸出,或者錯誤輸出,本來都要打印到屏幕上面的)  重定向其它輸出設備(文件,打開文件操作符,或打印機等等)1,2分別是標准輸出,錯誤輸出。

實例:

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
#顯示當前目錄文件 test.sh test1.sh test1.sh實際不存在
[chengmo@centos5 shell]$ ls  test .sh test1.sh
ls : test1.sh: 沒有這個文件和目錄
test .sh
 
#正確輸出與錯誤輸出都顯示在屏幕了,現在需要把正確輸出寫入suc.txt
# 1>可以省略,不寫,默認所至標准輸出
[chengmo@centos5 shell]$ ls  test .sh test1.sh 1>suc.txt
ls : test1.sh: 沒有這個文件和目錄
[chengmo@centos5 shell]$ cat  suc.txt
test .sh
 
#把錯誤輸出,不輸出到屏幕,輸出到err.txt
[chengmo@centos5 shell]$ ls  test .sh test1.sh 1>suc.txt 2>err.txt
[chengmo@centos5 shell]$ cat  suc.txt err.txt
test .sh
ls : test1.sh: 沒有這個文件和目錄
#繼續追加把輸出寫入suc.txt err.txt  “>>”追加操作符
[chengmo@centos5 shell]$ ls  test .sh test1.sh 1>>suc.txt 2>>err.txt
 
#將錯誤輸出信息關閉掉
[chengmo@centos5 shell]$ ls  test .sh test1.sh 2>&-
test .sh
[chengmo@centos5 shell]$ ls  test .sh test1.sh 2> /dev/null
test .sh
#&[n] 代表是已經存在的文件描述符,&1 代表輸出 &2代表錯誤輸出 &-代表關閉與它綁定的描述符
#/dev/null 這個設備,是linux 中黑洞設備,什么信息只要輸出給這個設備,都會給吃掉
 
#關閉所有輸出
[chengmo@centos5 shell]$ ls  test .sh test1.sh  1>&- 2>&-
#關閉 1 ,2 文件描述符
[chengmo@centos5 shell]$ ls  test .sh test1.sh  2> /dev/null  1> /dev/null
#將1,2 輸出轉發給/dev/null設備
[chengmo@centos5 shell]$ ls  test .sh test1.sh > /dev/null  2>&1
#將錯誤輸出2 綁定給 正確輸出 1,然后將 正確輸出 發送給 /dev/null設備  這種常用
<p>[chengmo@centos5 shell]$ ls  test .sh test1.sh &> /dev/null
#& 代表標准輸出 ,錯誤輸出 將所有標准輸出與錯誤輸出 輸入到/dev/null文件
< /p >

 

 

注意:

1、shell遇到”>”操作符,會判斷右邊文件是否存在,如果存在就先刪除,並且創建新文件。不存在直接創建。 無論左邊命令執行是否成功。右邊文件都會變為空。

2、“>>”操作符,判斷右邊文件,如果不存在,先創建。以添加方式打開文件,會分配一個文件描述符[不特別指定,默認為1,2]然后,與左邊的標准輸出(1)或錯誤輸出(2) 綁定。

3、當命令:執行完,綁定文件的描述符也自動失效。0,1,2又會空閑。

4、一條命令啟動,命令的輸入,正確輸出,錯誤輸出,默認分別綁定0,1,2文件描述符。

5、一條命令在執行前,先會檢查輸出是否正確,如果輸出設備錯誤,將不會進行命令執行

  • 輸入重定向

格式:

command-line [n] <file或文件描述符&設備

將然有,命令默認從鍵盤獲得的輸入,改成從文件,或者其它打開文件以及設備輸入。執行這個命令,將標准輸入0,與文件或設備綁定。將由它進行輸入。

實例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[chengmo@centos5 shell] # cat > catfile
testing
cat  file  test
#這里按下 [ctrl]+d 離開
#從標准輸入【鍵盤】獲得數據,然后輸出給catfile文件
 
[chengmo@centos5 shell]$ cat >catfile < test .sh
#cat 從test.sh 獲得輸入數據,然后輸出給文件catfile
 
 
[chengmo@centos5 shell]$ cat >catfile <<eof
test  a file
test !
eof
 
#<< 這個連續兩個小符號, 他代表的是『結束的輸入字符』的意思。這樣當空行輸入eof字符,輸入自動結束,不用ctrl+D

 

 

  • exec綁定重定向

格式:

exec 文件描述符[n] <或> file或文件描述符或設備

在上面講的輸入,輸出重定向 將輸入,輸出綁定文件或設備后。只對當前那條指令是有效的。如果需要在綁定之后,接下來的所有命令都支持的話。就需要用exec命令

實例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[chengmo@centos5 shell]$ exec  6>&1
#將標准輸出與fd 6綁定
 
[chengmo@centos5 shell]$ ls   /proc/self/fd/ 
0  1  2  3  6
#出現文件描述符6
 
[chengmo@centos5 shell]$ exec  1>suc.txt
#將接下來所有命令標准輸出,綁定到suc.txt文件(輸出到該文件)
 
[chengmo@centos5 shell]$ ls  -al
#執行命令,發現什么都不返回了,因為標准輸出已經輸出到suc.txt文件了
 
[chengmo@centos5 shell]$ exec  1>&6
#恢復標准輸出
 
 
[chengmo@centos5 shell]$ exec  6>&-
#關閉fd 6描述符
 
[chengmo@centos5 ~]$ ls  /proc/self/fd/
0  1  2  3

說明:使用前先將標准輸入保存到文件描述符6,這里說明下,文件描述符默認會打開0,1,2 還可以使用自定義描述符 。然后對標准輸出綁定到文件,接下來所有輸出都會發生到文件。 使用完后,恢復標准的輸出,關閉打開文件描述符6。

有趣事情:

可能有朋友會這樣用:exec 1>suc.txt ,接下來所有輸出都綁定到suc.txt 文件,那么怎么樣恢復原來的呢? 試試你就會發現問題所在……

  • 復雜一點實例
1
2
3
4
5
6
7
8
9
10
11
exec  3<> test .sh;
#打開test.sh可讀寫操作,與文件描述符3綁定
 
while  read  line<&3
  do
     echo  $line;
done
#循環讀取文件描述符3(讀取的是test.sh內容)
exec  3>&-
exec  3<&-
#關閉文件的,輸入,輸出綁定

 

  • 總結下:

學習就要總結,總結才可以提高了。哈哈!

估計還有一些朋友是頭暈暈的。怎么linux的重定向這么復雜呢,又是文件打開描述符又是讀,還有些,還有默認標准輸入輸出。

其實,總結一下,重定向應用通常就以下兩點:

1、重新設置命令的默認輸入,輸出,指向到自己文件(文件,文件描述符,設備其實都是文件,因為linux就是基於設備也是文件,描述符也指向是文件,哈哈)

2、擴展自己新的描述符,對文件進行讀寫操作

 
[Mid]

這次我們看下管道命令了。shell管道,可以說用法就簡單多了。

 

管道命令操作符是:”|”,它僅能處理經由前面一個指令傳出的正確輸出信息,也就是 standard output 的信息,對於 stdandard 
error 信息沒有直接處理能力。然后,傳遞給下一個命令,作為標准的輸入 standard input.

 

  • 管道命令使用說明:

先看下下面圖:

image

command1正確輸出,作為command2的輸入 然后comand2的輸出作為,comand3的輸入 ,comand3輸出就會直接顯示在屏幕上面了。

通過管道之后:comand1,comand2的正確輸出不顯示在屏幕上面

注意:

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

2、管道命令右邊命令,必須能夠接收標准輸入流命令才行。

實例:

 

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 等等,都是些文本處理命令。

  • 管道命令與重定向區別

區別是:

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

 

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

這些都是網上總結很多的,其實只要多加清楚用法,也一定有自己的一份不同描述。

實例:

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 通過出入文件名讀取文件內容
 
[End]
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM