標題: linux腳本之>/dev/null 2>&1,以及2>1 VS 2>&1 [打印本頁]

作者: 51黑tt    時間: 2016-3-6 15:19
標題: linux腳本之>/dev/null 2>&1,以及2>1 VS 2>&1
                                                                                                1. 標準輸入stdin文件描述符為0,標準輸出stdout文件描述符為1,標準錯誤stderr文件描述符為2
2. /dev/null 空設備,相當于垃圾桶
3. 重定向符號:>
3. 2>1 與 2>&1 的區(qū)別
   2>1, 把標準錯誤stderr重定向到文件1中
   2>&1,把標準錯誤stderr重定向到標準輸出stdout
4. 舉例:
   假設有腳本test.sh,內(nèi)容如下,t是一個不存在的命令,執(zhí)行腳本進行下面測試。
   # 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文件,重新執(zhí)行,這次是把標準輸出定向到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
   #







歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1