close

結論:
( commnad | tee stdout-by-tee.log ) 1> >(tee st-all.log) 2> >(tee stderr.log)
((commnad) 1> >(tee std.log)) 2> >(tee str.log) | tee tee-output.log

E.g.: (ipmitool -I lanplus -U admin -P admin -H 192.168.20.173 fru print | tee stdout-by-tee.log ) 1> >(tee st-all.log) 2> >(tee stderr.log)

一般而言,跑script的同時,需要在Terminal顯示Output的同時
,又需要將畫面上的資料儲存至文字檔的時候,由於 command > $LogFile 會將command的結果,直接儲存至$LogFile而不會顯示在畫面上
,因此會用tee進行儲存或是tee -a 附加到紀錄黨內

但tee這組指令的特性是,僅將標準輸出儲存到檔案上,意味著只儲存所謂的Standard Output (stdout)
那需要將錯誤訊息Standard Error (stderr)記錄下來的話,怎麼辦呢? 
這時候2>&1 就是一個好用的東西,而且是很常見的方法
使用2>&1 | tee -a $Log,透過 2>&1 將Error Message Redirection至Standard Output

上述的方式,會得到"Standard Output and Error"儲存至檔案且又能同時顯示輸出

But 問題來了,當需要 stdout , stderror , stderror , stdout&stderror 同時顯示在畫面上 , 且透過 檢查 $? 確認Command 是否正常執行。
解決的方法如結論。


查資料的時候,在Ref這個網站上看到這個範例 ((ls -C; cmd_with_err) 1> >(tee /tmp/out.log)) 2> >(tee /tmp/err.log 2>&2)
,看似可行,套上我要的command: ((ipmitool -I lanplus -U admin -P admin -H 192.168.20.173 fru list) 1> >(tee sto.log)) 2> >(tee str.log 2>&2) | tee sdtee.log
,檢查$?,也符合確認command執行有沒有錯誤

但我覺得Code看起來很醜,感覺不好維護。
試著將()重新組裝後,( commnad | tee stdout-by-tee.log ) 1> >(tee st-all.log) 2> >(tee stderr.log)
,檢查$?,也符合確認command執行有沒有錯誤
,我個人視覺上覺得比較好閱讀跟使用,我就這樣用了XD

 

Ref: https://www.tutorialspoint.com/redirect-output-of-process-to-a-file-and-streams

arrow
arrow

    吾給力 發表在 痞客邦 留言(0) 人氣()