BSD的sh和csh的重定向操做符全解

預先的知識一:        
        noclobber 禁止覆蓋變量,設定 $noclobber 預設變量改變輸出重定向特性
   
  1. 變量設定語法 set noclobber
  2.     取消變量設定語法 unset noclobber
複製代碼

    這個 noclobber 變量,它的功能即是中止重定向符號「>」的覆蓋(overwiting)已存在文件以及符號「>>」要將字符寫入一個不存在的文件時,自動產生該文件的特性。
        
        僅用兩個例子讓讀者明白,設定後的實際使用情況。
        例子一:
      
  1. % ps axu > testfile
  2.         % set noclobber
  3.         % echo "test set noclobber" > testfile
  4.         testfile: File exists.
  5.         % echo "test set noclobber" >! testfile
  6.         %
複製代碼


        例子二:
      
  1. % set noclobber
  2.         % cat /etc/passwd >> nopass
  3.         nopass: No such file or directory
  4.         % cat /etc/passwd >>! nopass
  5.         %
複製代碼


預先的知識二:        
        輸入輸出位置文件句柄:
        STDIN(標準輸入/Standard Input)                0
        STDOUT(標準輸出/Standard output)        1
        STDERR(標準錯誤/Standard error)                2

>file                  csh, sh          
        將 STDOUT(標準輸出/Standard output) 重定向到文件
         (command > file)

>>file                 csh, sh         
        將 STDOUT(標準輸出/Standard output) 字符串加到文件內容以後
         (command >> file)

<file                 csh, sh         
        將文件重定向到 STDIN(標準輸入/Standard Input) 做爲命令的輸入
         (command < file)

<<word                 csh, sh         
        讀取在線輸入直到word(結束輸入時,結束行輸入word), 並作輸入內容的變量替換
         (command <<word)

<<\word         csh, sh         
        讀取在線輸入直到word(結束輸入時,結束行輸入word), 不作輸入內容的變量替換
         (command <<\word)

<<-word         sh                         
        讀取在線輸入直到word(結束輸入時,結束行輸入word), 忽略TABS(製表符)
         (command <<-word)

>>!file         csh                 
        將 STDOUT(標準輸出/Standard output) 字符串追加到文件內容以後,當設定 $noclobber 時,可重寫文件。
         (command >>! file)

>!file                 csh                 
        將 STDOUT(標準輸出/Standard output) 重定向到新文件,當設定 $noclobber 時,可重寫文件。
         (command >! file)

>&file                 csh                 
        將 STDOUT(標準輸出/Standard output) 及 STDERR(標準錯誤/Standard error) 重定向到文件
         (command >& file)

>>&                csh                        
        將 STDOUT(標準輸出/Standard output) 及 STDERR(標準錯誤/Standard error) 字符串追加到文件內容以後
         (command >>& file)

<&digit         sh                         
        切換 STDIN(標準輸入/Standard Input) 到文件句柄
         (command >cmd.log 2<&1)

<&-                 sh                         
        關閉 STDIN(標準輸入/Standard Input)
         (command <&-)

>&digit         sh                         
        切換 STDOUT(標準輸出/Standard output) 到文件句柄
         (command >cmd.log 2>&1)

>&-                 sh                         
        關閉 STDOUT(標準輸出/Standard output)
         (command >&-)

>&!                csh                        
        將 STDOUT(標準輸出/Standard output) 及 STDERR(標準錯誤/Standard error) 重定向到文件,當設定 $noclobber 時,可重寫文件。
         (command >&! file)

>>&!                csh                        
        將 STDOUT(標準輸出/Standard output) 及 STDERR(標準錯誤/Standard error) 字符串加到文件內容以後,當設定 $noclobber 時,可重寫文件。
         (command >>&! file)
相關文章
相關標籤/搜索