2010-03-12 24 views

回答

11

annotate-output,從Debiandevscripts,這樣做。

在其手冊頁的例子:

 
$ annotate-output make 
21:41:21 I: Started make 
21:41:21 O: gcc -Wall program.c 
21:43:18 E: program.c: Couldn't compile, and took me ages to find out 
21:43:19 E: collect2: ld returned 1 exit status 
21:43:19 E: make: *** [all] Error 1 
21:43:19 I: Finished with exitcode 2 
+1

選擇了這個答案,因爲它在維持秩序方面更好,儘管你必須一直衝洗輸出...... – 2010-03-13 19:15:23

10

嘗試這種情況:

(myCommand | sed s/^/stdout:/ >> myLogfile) 2>&1 | sed s/^/stderr:/ >> myLogFile 

第1配管插入一個stdout:前綴的myCommand標準輸出,並將其追加到myLogFile

圓括號用於製作所有這些命令。他們表示,進一步的重定向適用於括號內的內容,而不適用於sed

然後將標準錯誤重定向到標準輸出2>&1(請記住原始標準輸出已重定向到myLogFile)。第二個管道插入stderr:前綴並將其附加到myLogFile

+0

我摸索着我對這種方式,但你是乾淨多了。 – 2010-03-12 12:31:34

+0

你能詳細解釋一下這個答案嗎?我想我明白了,除了括號... – 2010-03-12 23:50:39

+0

爲方便起見,'2&&1 |'可以寫成'|&'在Bash 4或zsh中。它與'> file'或'>&file'是有些對稱的,它們是'> file 2>&1'的快捷方式。 – ephemient 2010-03-13 07:14:59

相關問題