【筆記整理】標準輸入輸出和管線 (Standard Input Output and Pipes)

摘要:【筆記整理】標準輸入輸出和管線 (Standard Input Output and Pipes)

 

一、Standard Input and Output:下列為常用的三個數值(總共有0~10,系統預設開啟三個)

 

     

 

     註:圖檔擷取至『http://blog.jangmt.com/2012/08/linux-pipeline.html

 

 1. 0:Standard Input (STDIN),預設為鍵盤輸入值

 2. 1:Standard Output (STDOUT),預設為終端機螢幕顯示輸出

 3. 2:Standard Error (STDERR),預設為終端機螢幕顯示輸出

 

二、Redirecting Output to a File:I/O 轉向概念,改變上述原預設值。

       下列為常用的Redirecting Output符號

 1. > Redirect Output,預設等於『1>』,即為『STDOUT轉向輸出』,如果該檔案已存在則會覆蓋。

 2. 2> :Redirect STDERR,如果該檔案已存在則會覆蓋。

 3. &> :Redirect all(0,1,2) output,轉向輸出到檔案

 4. 2>&1 Redirect STDERR to STDOUT

  01. find /etc -name passwd > find.out 2> find.err:利用多個Argument將STDOUT、STDERR輸出到不同檔案

  02. find /etc -name passwd > find.out 2>&1:將『STDOUT』輸出至『find.out』,再透過『2>&1』,所以在

     『find.out』中為『STDOUT』和『STDERR』。

 

  03. find /etc -name passwd 2>&1 >find.out:將『STDERR』輸出至螢幕,而『STDOUT』轉向至『find.out

 5. >> :Append,如果該檔案已存在,則會添加在檔案後端

 6. | :Pipe

 7. < Redirect STDIN

 8. << :End-Word

 

三、Redirecting STDOUT to a Program (PIPING)

     用法:command1 | command2:將command1輸出導入到command2的輸入

 

     

 

     註:圖檔擷取至『http://blog.jangmt.com/2012/08/linux-pipeline.html

 

 1. ifconfig | grep Mask:將『ifconfig』輸出導入『grep Mask』的輸入,得到顯示『Mask』的訊息

 2. ifconfig | grep Mask | cut -f2 -d: | cut -f1 -d" ":結合多個指令或參數使用

 

四、Redirecting to Multiple Targets (tee):雙向重導向,多目標轉移

     用法:tee [ -a ] file:將『STDIN』輸出至『file』及『STDOUT

 

     

 

     註:圖檔擷取至『http://linux.vbird.org/linux_basic/0320bash.php#tee

 

 1. ifconfig | grep Mask | tee my.nic | cut -f2 -d: | cut -f1 -d" ":將『grep Mask』輸出的內容導入到

     『my.nic』中

     註:指令『tee』常用來與『| (Pipe)』配合進行debug

 2. ls -l | tee -a output:將『ls -l』的結果累加到檔案『output』中並顯示於螢幕上

 

五、Redirecting STDIN From a file (tr)

 1. tr 'a-z' 'A-Z' < .bash_profile:透過指令『tr』將檔案『.bash_profile』轉換大小寫後輸出至螢幕上

     註:關於指令『tr』的應用請參考

     『http://www.dotblogs.com.tw/ghoseliang/archive/2013/02/22/91671.aspx

     『tr:字串處理,字元轉換與刪除』

 

 

參考資料

http://www.csie.nctu.edu.tw/~tsaiwn/course/introcs/history/linux/linux.tnc.edu.tw/techdoc/shell/c668.html

http://blog.jangmt.com/2012/08/linux-pipeline.html

http://www.weithenn.org/cgi-bin/wiki.pl?Unit7-Standard_Input_Output_and_Pipes

 有空看看

https://app.shopback.com/RBANl9P3Ysb