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

linux啟動后,會默認打開3個文件描述符,分別是:標准輸入standard input 0,正確輸出standard output 1,錯誤輸出:error output 2。以后每次用新的進程打開文件,都會綁定文件描述符,因為子進程都會繼承父進程的詳細信息,如:環境變量。對於任何一條Linux命令執行,都會是這樣一個過程:
1、首先有一個標准輸入:Command.
2、然后根據這個標准輸入會有一個輸出結果:標准輸出與標准錯誤。
3、如果成功了會將正確的內容輸出到屏幕。
4、如果錯誤了也會將錯誤的內容輸出到屏幕,並且進行提示。
5、這些默認的輸出,輸入都是linux系統內定的,我們在使用過程中,有時候並不希望執行結果輸出到屏幕。我想輸出到文件或其它設備。這個時候我們就需要進行輸出重定向了。如下圖:
常用操作符
1、 標准輸入(stdin):代碼為0,使用<或者<< ;/dev/stdin -> /proc/self/fd/0 2、 標准輸出(stdout):代碼為1,使用>或者>>; /dev/stdout -> /proc/self/fd/1 3、 標准錯誤(stderr)代碼為2,使用2>或者2>>;/dev/stderr -> /proc/self/fd/2
輸出重定向
格式如下:
command-line1 [1-n]> file或文件操作符或設備
上面命令意思是:將一條命令執行結果(標准輸出,或者錯誤輸出,本來都要打印到屏幕上面的) 重定向其它輸出設備(文件,打開文件操作符,或打印機等等)1,2分別是標准輸出,標准錯誤。
示例如下:
實例一:顯示當前目錄文件 test.sh test1.sh,test1.sh實際不存在 [root@asus-a53s data]# touch test.sh [root@asus-a53s data]# ls test.sh test1.sh ls: 無法訪問test1.sh: 沒有那個文件或目錄 test.sh 實例二:正確輸出與錯誤輸出都顯示在屏幕了,現在需要把正確輸出寫入suc.txt。 [root@asus-a53s data]# ls test.sh test1.sh > suc.test #標准輸出代表的文件描述符可以省略 ls: 無法訪問test1.sh: 沒有那個文件或目錄 [root@asus-a53s data]# cat suc.test test.sh 實例三:把錯誤輸出,不輸出到屏幕,輸出到err.txt [root@asus-a53s data]# ls test.sh test1.sh > suc.test 2> err.txt [root@asus-a53s data]# cat suc.test err.txt test.sh ls: 無法訪問test1.sh: 沒有那個文件或目錄 實例四:繼續追加把輸出追加到suc.txt、err.txt [root@asus-a53s data]# ls test.sh test1.sh >> suc.test 2>> err.txt [root@asus-a53s data]# cat suc.test err.txt test.sh test.sh ls: 無法訪問test1.sh: 沒有那個文件或目錄 ls: 無法訪問test1.sh: 沒有那個文件或目錄 實例五:將錯誤輸出信息關閉掉 [root@asus-a53s data]# ls test.sh test1.sh 2>&- #&-代表關閉與它綁定的描述符,不輸出。 test.sh [root@asus-a53s data]# ls test.sh test1.sh 2> /dev/null #/dev/null是linux下面的一個及時刪除回收站 test.sh 實例六:關閉所有輸出 [root@asus-a53s data]# ls test.sh test1.sh 1>&- 2>&- #直接關閉兩個輸出 [root@asus-a53s data]# ls test.sh test1.sh > /dev/null 2>&1 #將正確與錯誤的輸出都重定向到指定設備 [root@asus-a53s data]# ls test.sh test1.sh &> /dev/null #同上,&> 等於2>&1
輸入重定向
格式如下:
command-line [n]<file或文件描述符&設備 將命令默認從鍵盤獲得的輸入,改成從文件,或者其它打開文件以及設備輸入。執行這個命令,將標准輸入0,與文件或設備綁定。將由它進行輸入。
示例如下:
實例一:用catfile文件里面的內容代替鍵盤輸出,輸出到屏幕。
[root@asus-a53s data]# cp suc.test catfile
[root@asus-a53s data]# cat < catfile
Ls #這里如果內容過多,按下 [ctrl]+d 離開
[root@asus-a53s data]# cat suc.test
ls
實例二:從標准輸入【鍵盤】獲得數據,然后輸出給catfile文件
[root@asus-a53s data]# cat > catfile << EOF #EOF可以是任何字符,代表結束
> hello,This is my test!
> EOF
[root@asus-a53s data]# cat catfile #上面用的是重定向覆蓋,所以ls內容不見了
hello,This is my test!
實例三:從test.sh 獲得輸入數據,然后輸出給文件catfile
[root@asus-a53s data]# cat test.sh
1
test1
test1
test1
[root@asus-a53s data]# cat > catfile < test.sh #這里在輸入的時候不能用<<,因為<<同時是也代表結束輸出
[root@asus-a53s data]# cat catfile
1
test1
test1
test1
綁定重定向
格式如下:
exec 文件描述符[n] <或> file或文件描述符或設備 在上面講的輸入,輸出重定向 將輸入,輸出綁定文件或設備后。只對當前那條指令是有效的。如果需要在綁定之后,接下來的所有命令都支持的話。就需要用exec命令
示例如下:
實例一:學習exec的綁定使用 [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/ 1 2 3
參考地址:https://www.cnblogs.com/chengmo/archive/2010/10/20/1855805.html