我有一個程序輸出到stdout和stderr,但沒有以正確的方式使用它們。有些錯誤會導致stdout,有些會發生stderr,非錯誤的東西會轉到stderr,並且會在stdout上輸出很多信息。爲了解決這個問題我想使管道做:如何使用stdout和stderr io-redirection從程序中獲取理智的錯誤/警告消息輸出?
- 保存的
$cmd
到一個文件$logfile
所有輸出(從兩個標準錯誤和標準輸出)(不打印到屏幕)。 - 過濾出stderr和stdout上的所有警告和錯誤消息(從警告|錯誤到空行)並着色僅「錯誤」字(將輸出重定向到stderr)。
- 將步驟2的輸出保存到文件
$logfile:r.stderr
。 - 從命令中退出並顯示正確的退出碼。
到目前爲止,我有這樣的:
$!/bin/zsh
# using zsh 4.2.0
setopt no_multios
# Don't error out if sed or grep don't find a match:
alias -g grep_err_warn="(sed -n '/error\|warning/I,/^$/p' || true)"
alias -g color_err="(grep --color -i -C 1000 error 1>&2 || true)"
alias -g filter='tee $logfile | grep_err_warn | tee $logfile:r.stderr | color_err'
# use {} around command to avoid possible race conditions:
{ eval $cmd } 2>&1 | filter
exit $pipestatus[1]
我嘗試過很多事情,但不能讓它開始工作。我讀過「從擊到Z殼牌」,許多職位,等我的問題是目前:
- 只有標準輸入進入過濾器
注:$cmd
是一個shell腳本調用前綴爲/usr/bin/time -p
的二進制文件。這似乎會導致管道問題,並且爲什麼我將命令包裝在{…}
中,所有輸出都進入管道。
'{eval $ cmd}'在zsh下工作,'$ logfile'出現在'filter '別名。 – Gilles 2011-06-10 23:20:50
Doah,謝謝你指出。 – shellter 2011-06-11 01:27:29