linux指令碼之>/dev/null 2>&1,以及2>1 VS 2>&1

來源:互聯網
上載者:User

1. 標準輸入stdin檔案描述符為0,標準輸出stdout檔案描述符為1,標準錯誤stderr檔案描述符為2

2. /dev/null 空裝置,相當於垃圾桶

3. 重新導向符號:>

3. 2>1 與 2>&1 的區別
   2>1, 把標準錯誤stderr重新導向到檔案1中
   2>&1,把標準錯誤stderr重新導向到標準輸出stdout

4. 舉例:
   假設有指令碼test.sh,內容如下,t是一個不存在的命令,執行指令碼進行下面測試。
   # cat test.sh
     t
     date

   標準輸出重新導向到log,錯誤資訊輸出到終端上,如下:
   # ./test.sh > log
     ./test.sh: line 1: t: command not found
   # cat log
     Thu Oct 23 22:53:02 CST 2008
   
   刪除log檔案,重新執行,這次是把標準輸出定向到log,錯誤資訊定向到檔案1
   # ./test.sh > log 2>1
   #
   # cat log
   Thu Oct 23 22:56:20 CST 2008
   # cat 1
   ./test.sh: line 1: t: command not found
   #

   把標準輸出重新導向到log檔案,把標準錯誤重新導向到標準輸出
   # ./test.sh > log 2>&1
   #
   # cat log
   ./test.sh: line 1: t: command not found
   Thu Oct 23 22:58:54 CST 2008
   #

   把錯誤資訊重新導向到空裝置
   # ./test.sh 2>/dev/null
   Thu Oct 23 23:01:07 CST 2008
   #
   
   把標準輸出重新導向到空裝置
   # ./test.sh >/dev/null
     ./test.sh: line 1: t: command not found

   把標準輸出和標準錯誤全重新導向到空裝置
   #./test.sh >/dev/null 2>&1
   #

 

 


   把標準輸出和標準錯誤全重新導向到空裝置
   #./test.sh >/dev/null 2>&1

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.