站長新書 站長開講 首頁 最新文章 站長著作及審校 FreeBSD 筆記 Linux 筆記 Windows 筆記 虛擬化筆記 網管人雜誌 遊山玩水 關於本站
站長新書 VMware vSphere ICM 團購開跑了!!

Unit7-Standard Input Output and Pipes

Standard Input and Output

以下為常用的 Standard Input and Output 數值 (實際上不只此三個數值而以,而是 0 ~ 10)

Redirecting Output to a File

以下為常用的 Redirecting Output 符號

例如下列操作中輸入的指令 ls /tmp 即為 Standard Input (0) 而查詢結果即為 Standard Output (1)

 #ls /tmp/                       //Standard Input (0)
 mapping-root                    //Standard Output (1)

接下來請使用一般使用者帳號 weithenn 來登入系統進行練習,如下操作完成後相信您會更了解何為 Standard Input、Standard Output、Standard Error。

 $find /etc -iname passwd                                            //Standard Input (0)
 find: /etc/racoon/certs: Permission denied                          //Standard Error (2)
 find: /etc/cups/ssl: Permission denied                              //Standard Error (2)
 find: /etc/audisp: Permission denied                                //Standard Error (2)
 find: /etc/selinux/targeted/modules/previous: Permission denied     //Standard Error (2)
 find: /etc/selinux/targeted/modules/active: Permission denied       //Standard Error (2)
 /etc/pam.d/passwd                                                   //Standard Output (1)
 /etc/passwd                                                         //Standard Output (1)
 find: /etc/lvm/backup: Permission denied                            //Standard Error (2)
 find: /etc/lvm/archive: Permission denied                           //Standard Error (2)
 find: /etc/lvm/cache: Permission denied                             //Standard Error (2)
 find: /etc/audit: Permission denied                                 //Standard Error (2)
 find: /etc/cron.d: Permission denied                                //Standard Error (2)
 find: /etc/pki/CA: Permission denied                                //Standard Error (2)
 $find /etc -name passwd > find.out                                  //Standard Input (0)
 find: /etc/racoon/certs: Permission denied                          //Standard Error (2)
 find: /etc/cups/ssl: Permission denied
 find: /etc/audisp: Permission denied
 find: /etc/selinux/targeted/modules/previous: Permission denied
 find: /etc/selinux/targeted/modules/active: Permission denied
 find: /etc/lvm/backup: Permission denied
 find: /etc/lvm/archive: Permission denied
 find: /etc/lvm/cache: Permission denied
 find: /etc/audit: Permission denied
 find: /etc/cron.d: Permission denied
 find: /etc/pki/CA: Permission denied
 $cat find.out                                                       //查看 find.out 檔案內容
 /etc/pam.d/passwd                                                   //剛才指令 Standard Output (1) 的結果
 /etc/passwd
 $find /etc -name passwd 2> find.out                                 //Standard Input (0)
 /etc/pam.d/passwd                                                   //Standard Output (1)
 /etc/passwd
 $cat find.out                                                       //查看 find.out 檔案內容
 find: /etc/racoon/certs: Permission denied                          //剛才指令 Standard Error (2) 的結果
 find: /etc/cups/ssl: Permission denied
 find: /etc/audisp: Permission denied
 find: /etc/selinux/targeted/modules/previous: Permission denied
 find: /etc/selinux/targeted/modules/active: Permission denied
 find: /etc/lvm/backup: Permission denied
 find: /etc/lvm/archive: Permission denied
 find: /etc/lvm/cache: Permission denied
 find: /etc/audit: Permission denied
 find: /etc/cron.d: Permission denied
 find: /etc/pki/CA: Permission denied
 $find /etc -name passwd > find.out 2> find.err
 $find /etc -name passwd > find.out 2>&1
 $find /etc -name passwd 2>&1 > find.out
 find: /etc/racoon/certs: Permission denied
 find: /etc/cups/ssl: Permission denied
 find: /etc/audisp: Permission denied
 find: /etc/selinux/targeted/modules/previous: Permission denied
 find: /etc/selinux/targeted/modules/active: Permission denied
 find: /etc/lvm/backup: Permission denied
 find: /etc/lvm/archive: Permission denied
 find: /etc/lvm/cache: Permission denied
 find: /etc/audit: Permission denied
 find: /etc/cron.d: Permission denied
 find: /etc/pki/CA: Permission denied

Redirecting STDOUT to a Program (Piping)

我們可利用管線 (|) Pipe 來使上個指令的輸出結果變成為下個指令的輸入,如下指令配合 Pipe 應該可輕鬆顯示目前網路卡上的 IP Address

 #ifconfig | grep Bc | cut -f2 -d: | cut -f1 -d" "
 192.168.1.10

也可使用 Pipe 來達成將 Mail 內容快速輸入的效果

 ex. mail -s "test3" weithenn          //輸入後按下 Enter
 Test Mail 3                           //郵件內容
 .                                     //表示郵件內容結束

Redirecting to Multiple Targets (tee)

tee 指令可以配合 Pipe 指令在輸出 Pipe 執行途中其結果方便您除錯,如下操作中利用 tee 指令將上一個 Pipe (grep Bc) 輸出內容導入 my.nic 檔案中,因此若 Pipe 指令出錯時可利用 tee 來進行除錯。

 #ifconfig | grep Bc | tee my.nic | cut -f2 -d: | cut -f1 -d" "
 192.168.1.10
 #cat my.nic
 inet addr:192.168.1.10  Bcast:10.10.25.255  Mask:255.255.255.0

Redirecting STDIN from a File (tr)

tr 指令可將指定檔案中其英文字元內容進行 大/小寫變換,如下操作將家目錄下 .bash_profile 檔案內容中所有英文大寫字母變更為小寫英文字母 (僅輸出至螢幕而非改變實際檔案內容)

 #tr 'A-Z' 'a-z' < ~/.bash_profile
 # .bash_profile
 # get the aliases and functions
 if [ -f ~/.bashrc ]; then
         . ~/.bashrc
 fi
 # user specific environment and startup programs
 path=$path:$home/bin
 export path
 unset username

Scripting for loops continued

我們可於 for 迴圈中透過 seq 來輕鬆輸出數字,並可透過 -w 參數來將對齊。

 #for i in $(seq 1 1 10); do echo ${i}; done
 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 #for i in $(seq -w 1 1 10); do echo ${i}; done
 01
 02
 03
 04
 05
 06
 07
 08
 09
 10
 #for i in $(seq -w 1 3 10); do echo ${i}; done
 01
 04
 07
 10
 #for i in $(seq -w 2 2 20); do echo ${i}; done
 02
 04
 06
 08
 10
 12
 14
 16
 18
 20
Go To Oddmuse OrgGo To FreeBSD OrgCreative Commons 2.5 Taiwansitestates.com