1. 程式人生 > >雲計算怎麽學?Io重定向Redirection詳解

雲計算怎麽學?Io重定向Redirection詳解

雲計算

Linux shell中的File Descripter可以理解為一個指向文件的指針。默認有三個FD:0,1,2。分別指向的是:Keyboard設備文件,Moniter設備文件,和Moniter設備文件。Shell中還允許有3--9的FD,默認都沒有打開,可以認為指向的為NULL。

可以通過以下命令查看打開的FD:ls /proc/self/fd返回的數字代表FD的值。利用重定向可以為一個FD賦值,使其指向一個非NULL的文件,其實就是打開一個FD。

6>&1

可以理解為將FD6指針指向FD1指針指向的文件,即Moniter。這樣,FD6和FD1就同時指向同一個文件:Moniter。6>&-可以理解為將FD6指針置為空值null,即關閉FD6。一個重定向只在當前命令中有效。exec可以使IO重定向在當前shell中長期有效。通過類似指針的特性,I/O重定向可以用來保存當前某個FD並在其後恢復。例如:

過濾stderr中的內容,屏蔽含有字符串“unused”的錯誤輸出:

創建FD6,指向FD1指向的moniter。exec 6>&1# 運行command命令,將stdout重定向給FD6,再將stderr重定向給stdout。# 這時,command有兩個輸出FD,# 分別是FD1和FD6,FD1是錯誤信息,FD6是標準輸出內容。# 再將FD1錯誤信息通過pipe傳給grep進行過濾,並將過濾後的內容重定向給FD2。# 最後,將之前的FD6的內容重定向給FD1。# 這樣,這條命令有兩個輸出,FD1是command的標準輸出內容,# FD2是經過過濾的command的錯誤輸出。(command 2>&1 >&6 | grep -v unused >&2) 6>&1# 關閉FD6。

exec 6>&-

cmd1|cmd2: pipe,將cmd1的標準輸出作為cmd2的標準輸入

· >file:將標準輸出重定向到file

· <file:將file作為標準輸入

· >>file:將標準輸出重定向到file,如果file存在,append到文件中,即附加到文件的後面,而不是覆蓋文件

當cat不帶參數的時候,表示使用標準輸入作為輸入,這允許在標準輸入中鍵入相關的內容,下面將alias加入.bashrc作為最後一行$ cat >> .bashrc alias cdmnt=‘mount -t iso9660 /dev/sbpcd /cdrom‘ ^D

· >|file:強制將標準輸出重定向到file,即使noclobber設置。當設置環境變量set –o noclobber,將禁止重定向到一個已經存在的文件中,避免文件被覆蓋。

· n >|file:強制將文件描述符n重定向到file,即使noclobber打開

· <>file:將file作為標準輸入和標準輸出。通常用於設備文件(/dev下面的文件),底層系統程序員可用之測試設備驅動,其他的很少用。

· n <>file:將file作為文件描述符n的輸入和輸出

· <<label:Here-document; see text 。將shell的標準輸入作為命令的輸入,直到行中之包含label。這中間的輸入成為here-document。下面是一個例子。我們讓人使用cat >> file的方式,通過標準輸入在文件中附加內容。

$ cat >> msgfile << . #這裏<<.表明以.為結束。因此無需使用^D,而改用. > this is the text of > our message. > .#這裏表示結束。則msgfile中增加了兩行this is…和our message.MACHINE="i586" OS="Linux-gnu" CC="gcc" cat > $file <Machine: $MACHINE OS: $OS Compiler: $CC EOF 查看:cat $file,這裏給出正常結構Machine: i586 OS: linux-gnu Compiler: gcc 如果在EOF加上單引號,或者雙引號如下cat > $file <<‘EOF‘ 則不解析$CC的內容,文件內容如下Machine: $MACHINE OS: $OS Compiler: $CC 如果使用<<-,如下,則刪除所有行中前面打頭的> tab<這樣在腳本的書寫上會比較適合閱讀 ,例如上面的例子可以寫為: cat > $file <<-EOF Machine: $MACHINE OS: $OS Compiler: $CC EOF

· n>file:將文件描述符n重定向到file

· n :將file作為文件描述符的輸入

· n>>file :將文件描述符n的輸出重定向到file,如果file存在,將輸出append到文件後面

· n>& :將標準輸出復制到文件描述符n(Duplicate standard output to file descriptor n)

· n<& :從文件描述符n復制標準輸入(Duplicate standard input from file descriptor n)

· n>&m :文件描述字n將一個copy至文件描述字m(File descriptor n is made to be a copy of the output file descriptor)

· n<&m :文件描述字n作為文件描述字m中的一個拷貝(File descriptor n is made to be a copy of the input file descriptor)

· &>file : 將標準輸出和標準錯誤輸出定向至文件file

· <&- : 關閉標準輸入

· >&- : 關閉標準輸出

· n>&- : 關閉文件描述字作為輸出(Close the output from file descriptor n)

· n<&- :關閉文件描述字作輸入(Close the input from file descriptor n)

· n>&word: If n is not specified, the standard output (file descriptor 1) is used. If the digits in word do not specify a file descriptor open for output, a redirection error occurs. As a special case, if n is omitted, and word does not expand to one or more digits, the standard output and standard error are redirected as described previously.

· n<&word : If word expands to one or more digits, the file descriptor denoted by n is made to be a copy of that file descriptor. If the digits in word do not specify a file descriptor open for input, a redirection error occurs. If word evaluates to -, file descriptor n is closed. If n is not specified, the standard input (file descriptor 0) is used.

· n>&digit- : Moves the file descriptor digit to file descriptor n, or the standard output (file descriptor 1) if n is not specified.

· n<&digit- : Moves the file descriptor digit to file descriptor n, or the standard input (file descriptor 0) if n is not specified. digit is closed after being duplicated to n.

文件描述符

文件描述符在bash中比較少用,從0開始用戶表示進行的數據流,0表示標準輸入,1表示標準輸出,2表示標註錯誤輸出,其他從3開始。最為常用的場景是將錯誤消息輸出到某個文件,可以加上2>file到我們的命令中。

我們來看下面一個腳本的例子:

command > logfile 2>&1 &

  >logfile,表示command的標準輸出重定向至文件logfile中,2>&1,匹配n>&m,表示文件描述字2(command的標準錯誤輸出)將copy一份采用文件描述字1(即標準輸出),由於標準輸出已經重定向logfile,這份copy也見將重定向至文件lofgile。我們可以用“abcd > logfile 2>&1 &”來驗證這個效果。最後&表示後臺運行的方式。這樣命令表示在後臺運行command,而它的標準輸出和錯誤輸出均重定向到logfile文件中。下面可達到類似的效果:

command 2>&1 | tee logfile &

錯誤輸出同樣適用標準輸出,通過pipe方式,見他們作為輸入執行tee logfile。tee命令將它的標準輸入copy至他的標準標準輸出以及參數所帶的文件中。和上面的命令不一眼這裏即會在stdout和logfile中同時輸出。

雲計算怎麽學?Io重定向Redirection詳解