linux之資料重新導向

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   os   使用   ar   div   

  1. 標準輸入  (stdin) :代碼為 0 ,使用 < 或 << ;
  2. 標準輸出  (stdout):代碼為 1 ,使用 > 或 >> ;
  3. 標準錯誤輸出(stderr):代碼為 2 ,使用 2> 或 2>> ;

 

  • 1> :以覆蓋的方法將『正確的資料』輸出到指定的檔案或裝置上;
  • 1>>:以累加的方法將『正確的資料』輸出到指定的檔案或裝置上;
  • 2> :以覆蓋的方法將『錯誤的資料』輸出到指定的檔案或裝置上;
  • 2>>:以累加的方法將『錯誤的資料』輸出到指定的檔案或裝置上;

根據鳥哥的資料,簡單記錄幾個小例子:

 

範例二:利用一般身份帳號搜尋 /home 底下是否有名為 .bashrc 的檔案存在[[email protected] ~]# su - dmtsai  <==假設我的系統有名為 dmtsai 的帳號[[email protected] ~]$ find /home -name .bashrc <==身份是 dmtsai 喔!find: /home/lost+found: Permission denied  <== Standard errorfind: /home/alex: Permission denied        <== Standard errorfind: /home/arod: Permission denied        <== Standard error/home/dmtsai/.bashrc                       <== Standard output

 

範例三:承範例二,將 stdout 與 stderr 分存到不同的檔案去[[email protected] ~]$ find /home -name .bashrc > list_right 2> list_error

 

範例四:承範例三,將錯誤的資料丟棄,螢幕上顯示正確的資料[[email protected] ~]$ find /home -name .bashrc 2> /dev/null/home/dmtsai/.bashrc  <==只有 stdout 會顯示到螢幕上, stderr 被丟棄了

 

範例五:將指令的資料全部寫入名為 list 的檔案中[[email protected] ~]$ find /home -name .bashrc > list 2> list  <==錯誤[[email protected] ~]$ find /home -name .bashrc > list 2>&1     <==正確[[email protected] ~]$ find /home -name .bashrc &> list         <==正確

上述表格第一行錯誤的原因是,由於兩股資料同時寫入一個檔案,又沒有使用特殊的語法, 此時兩股資料可能會交叉寫入該檔案內,造成次序的錯亂。所以雖然最終 list 檔案還是會產生,但是裡面的資料排列就會怪怪的,而不是原本螢幕上的輸出排序。 至於寫入同一個檔案的特殊語法如上表所示,你可以使用 2>&1 也可以使用 &> ! 一般來說,鳥哥比較習慣使用 2>&1 的語法啦!

 

原文地址:http://linux.vbird.org/linux_basic/0320bash.php#redirect

linux之資料重新導向

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.