簡體   English   中英

bash腳本中的grep stderr用於任何輸出

[英]Grep stderr in bash script for any output

在我的bash腳本中,我在類似以下的不同日志中使用grep:

LOGS1=$(grep -E -i 'err|warn' /opt/backup/exports.log /opt/backup/imports.log && grep "tar:" /opt/backup/h2_backups.log /opt/backup/st_backups.log)

if [ -n "$LOGS1" ] ]; then
        COLOUR="yellow"
        MESSAGE="Logs contain warnings. Backups may be incomplete. Invetigate these warnings:\n$LOGS"

我想檢查腳本是否運行,而不是檢查每個日志是否存在(比這還多的日志),所以我想檢查stderr以查看是否獲得任何輸出。 如果其中一個日志不存在,則會產生如下錯誤: grep: /opt/backup/st_backups.log: No such file or directory

我試圖用命令command 2> >(grep "file" >&2類的command 2> >(grep "file" >&2讀取sterr,但這似乎不起作用。

我知道我可以將輸出傳遞到文件,但是我寧願在有任何輸出時處理stderr而不是讀取文件。 或有什么理由可以更好地歸檔文件?

將標准錯誤(文件描述符2)發送到標准輸出(文件描述符1)並將其分配給var Q:

$ Q=$(grep text file 2>&1)
$ echo $Q
grep: file: No such file or directory

這是默認行為,通常將stderr設置為您的終端(並且沒有緩沖),因此在將stdout傳輸到某處時會看到錯誤。 如果您想將stderr與stdout合並,則使用以下語法:

command >file 2>&1

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM