出於特殊目的,bash shell保留了最早的3個文件描述符0、1、2,如下所示:
1、STDIN標准輸入
Shell從STDIN文件描述對應的鍵盤獲得輸入,在用戶輸入時處理每個字符。
范例1:cat命令處理STDIN輸入的數據,輸入一個就顯示一個,按ctrl+c即可結束。
[root@cloucentos6 home]# cat
my
my
name
name
is
is
TOM
TOM
范例2:使用輸入重定向符號 < 時,Linux會重寫向指定的文件來替換文字的輸入文件描述符。
[root@cloucentos6 home]# cat file
my name is Tom.
[root@cloucentos6 home]# cat < file
my name is Tom.
2、STDOUT標准輸出
shell所有的輸出(包括shell中運行的程序和腳本)會被定向到標准輸出中,就是在終端上顯示出來。
范例1:使用輸出重定向 > 來改變輸出。
[root@cloucentos6 home]# ls -l > file
[root@cloucentos6 home]# cat file
總用量 20
-rw-r--r--. 1 root root 0 6月 24 07:37 file
drwx------. 2 root root 16384 12月 21 2016 lost+found
-rwx------. 1 root root 81 6月 23 23:32 test.sh
范例2:使用輸出重寫向 >> 可以追加某個文件。
[root@cloucentos6 home]# ls -l >> file
[root@cloucentos6 home]# cat file
總用量 20
-rw-r--r--. 1 root root 0 6月 24 07:37 file
drwx------. 2 root root 16384 12月 21 2016 lost+found
-rwx------. 1 root root 81 6月 23 23:32 test.sh
總用量 24
-rw-r--r--. 1 root root 171 6月 24 07:37 file
drwx------. 2 root root 16384 12月 21 2016 lost+found
-rwx------. 1 root root 81 6月 23 23:32 test.sh
3、STDERR標准錯誤
shell運行程序和腳本出錯時生成的錯誤消息都會發送到這個位置。
范例1:只重定向錯誤,STERR文件描述符被設成2,ls 查看一個/home目錄不存在的文件將錯誤輸出到file文件,屏幕上不會顯示錯誤。
[root@cloucentos6 home]# ls -l test4 2>file
[root@cloucentos6 home]# cat file
ls: 無法訪問test4: 沒有那個文件或目錄
范例2:重寫向錯誤和數據,如果想重寫向錯誤和正常輸出,必須用兩個重寫向符號。STDOUT文件描述符被設為1,STERR文件描述符被設成2。
[root@cloucentos6 home]# ls
lost+found test.sh
[root@cloucentos6 home]# ls -l test.sh test4 2>file1 1>file2
[root@cloucentos6 home]# ls
file1 file2 lost+found test.sh
[root@cloucentos6 home]# cat file1
ls: 無法訪問test4: 沒有那個文件或目錄
[root@cloucentos6 home]# cat file2
-rwx------. 1 root root 81 6月 23 23:32 test.sh
范例3:假如把STDERR和STDOUT輸出重定向到同一個文件file中,bash shell提供了一個特殊的重定向符號 &>
[root@cloucentos6 home]# ls
lost+found test.sh
[root@cloucentos6 home]# ls -l test.sh test2 &>file
[root@cloucentos6 home]# cat file
ls: 無法訪問test2: 沒有那個文件或目錄
-rwx------. 1 root root 81 6月 23 23:32 test.sh
4、在腳本中重寫向輸出
范例1:在腳本中臨時指定某行重定向輸出標准錯誤和標准
[root@cloucentos6 home]# cat test.sh
#!/bin/bash
bit=`arch`
echo "hello ! world~!"
echo "系統位數 $bit" >&2
ipocnfig >&2
[root@cloucentos6 home]# ./test.sh 2>out
hello ! world~!
[root@cloucentos6 home]# cat out
系統位數 x86_64
./test.sh: line 5: ipocnfig: command not found
范例2:永久重定向輸出腳本中所有標准錯誤
[root@cloucentos6 home]# cat test.sh
#!/bin/bash
exec 2>/home/out
bit=`arch`
echo "hello ! world~!"
echo "系統位數 $bit"
ipocnfig
[root@cloucentos6 home]# ./test.sh
hello ! world~!
系統位數 x86_64
[root@cloucentos6 home]# cat out
./test.sh: line 7: ipocnfig: command not found
范例3:永久重定向輸出腳本中所有標准錯誤和標准輸出
[root@cloucentos6 home]# cat test.sh
#!/bin/bash
exec &>/home/out
bit=`arch`
echo "hello ! world~!"
echo "系統位數 $bit"
ipocnfig
[root@cloucentos6 home]# ./test.sh
[root@cloucentos6 home]# cat out
hello ! world~!
系統位數 x86_64
./test.sh: line 7: ipocnfig: command not found
5、阻止命令輸出
如果你想shell腳本后台運行輸出所有的信息都要丟掉,不想顯示出來,可以把Shell重定向到一個稱作null文件的特殊文件,只要輸出到null文件的任何數據都不會保存,這樣所有的輸出都會被丟掉。
范例1:Linux系統上的null文件的標准位置是/dev/null,你重定向到該位置的任何數據都會被丟掉,不會顯示:
[root@cloucentos6 home]# ls -l > /dev/null
[root@cloucentos6 home]# cat /dev/null
范例2:阻止任何錯誤消息而不保存它們的一個通用方法
[root@cloucentos6 home]# ls -al test4.txt test.sh 2>/dev/null
-rwx------. 1 root root 98 6月 26 19:09 test.sh
范例3:由於/dev/null文件不含有任何內容,重定向輸入將/dev/null作為輸入文件,可以使用它快速清空現有文件中的所有數據,不用先刪除文件再創建。
[root@cloucentos6 home]# cat out
hello ! world~!
[root@cloucentos6 home]# cat /dev/null > /home/out
[root@cloucentos6 home]# cat out
6、記錄消息
tee命令相當於管道的一個T型接頭,可以從標准輸入過來的數據同時發給兩個上的地。一個目的地是標准輸出,另一個目的地是tee命令行所指定的文件名。
范例1:tee將從STDIN過來的數據重定向。
[root@cloucentos6 home]# date | tee out
2017年 06月 27日 星期二 00:18:28 CST
[root@cloucentos6 home]# cat out
2017年 06月 27日 星期二 00:18:28 CST
范例2:默認情況下,tee命令會在每次使用時覆蓋輸出文件,如果想將數據追加到文件中,必須使用 – a 選項。
[root@cloucentos6 home]# cat out
2017年 06月 27日 星期二 00:18:28 CST
root@cloucentos6 home]# who | tee -a out
root pts/0 2017-06-27 00:14 (10.8.9.11)
[root@cloucentos6 home]# cat out
2017年 06月 27日 星期二 00:18:28 CST
root pts/0 2017-06-27 00:14 (10.8.9.11)
[root@cloucentos6 home]# arch | tee out
x86_64
[root@cloucentos6 home]# cat out
x86_64
范例3:腳本中把數據保存到文件中,也能把數據顯示在屏幕上(但tee命令輸出的錯誤信息不會保存在文件中)。
[root@cloucentos6 home]# cat test.sh
#!/bin/bash
bit=`arch`
echo "hello ! world~!" | tee out
echo "系統位數 $bit" | tee -a out
ipocnfig | tee -a out
[root@cloucentos6 home]# ./test.sh
hello ! world~!
系統位數 x86_64
./test.sh: line 5: ipocnfig: command not found
[root@cloucentos6 home]# cat out
hello ! world~!
系統位數 x86_64