1. 程式人生 > >Linux Bash Shell學習 十七 I/O重定向

Linux Bash Shell學習 十七 I/O重定向

               

  本文也即《Learning the bash Shell》3rd Edition的第七章Input/Output and Command-Line Processing之讀書筆記之一。我們曾經學習過shell的基本IO重定向操作:>、<和|。基本上能滿足95%的情況,但是我們需要知道bash支援的重定向操作。

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中同時輸出。

其他檔案描述字的重定向,例如<&n,通常用於從多個檔案中讀入或者寫出。

<&-,表示強制關閉標準輸入           >&-,表示強制關閉標準輸出           1>,等同於>           0<,等同於<