2011-10-26 25 views
1

錯誤信息總是被打印到終端每當我得到的bash shell(例如窮人語法)錯誤

$ totem Desktop/songs/song1.mp3 
** Message: Error: Resource not found. 
gstfilesrc.c(1055): gst_file_src_start(): /GstPlayBin2:play/GstURIDecodeBin:uridecodebin0/GstFileSrc:source: 
No such file "/home/me/Desktop/songs/song1.mp3" 

那麼什麼是重定向標準錯誤,如果標準錯誤會直接默認打印到終端到stdout的目的是什麼?

$ command-name 2>&1 

回答

3

默認情況下,stdoutstderr定向到終端。如果您將stderr重定向到stdout,那麼它也會轉到終端。

如果你想擺脫標準錯誤輸出,重定向到無效:

$ command-name 2> /dev/null 
+1

@ Didier Trosset。謝謝!那麼,爲什麼我要將stderr重定向到stdout呢? – user784637

+1

@LedZeppelin:如果你希望輸出和錯誤都記錄在同一個文件中;)這不是我要做的事情,但情況可能要求它 – Sujoy

+0

你不能將輸出和錯誤都重定向到同一個文件用'$ command1&> errorAndOutput'? – user784637

2

請注意,stdout默認打印到終端。並通過做command-name 2>&1你基本上重定向stderr到相同的地方。

重定向的順序也很重要。

那麼你正在尋找的是,command-name >file_for_stdout 2>&1

1

stderrstdout默認情況下都控制終端上顯示。如果你像你這樣多路複用它們,它們都會被輸出到任何stdout輸出端。然後,您可以將stdout重定向到一個文件,以在單個文件中獲取輸出和錯誤。

$ command >log 2>&1 

將把錯誤和輸出放到名爲`log的文件中。

相關問題