在經過管道使用「 tee」時如何將stderr寫入文件?

我知道如何使用teeaaa.sh的輸出( STDOUTaaa.sh bbb.out ,同時仍將其顯示在終端中: html

./aaa.sh | tee bbb.out

如今如何將STDERR寫入一個名爲ccc.out的文件,同時仍然顯示它? shell


#1樓

換句話說,您但願將stdout傳遞到一個過濾器( tee bbb.out ),將stderr tee bbb.out給另外一個過濾器( tee ccc.out )。 除了將stdout傳送到另外一個命令外,沒有其餘標準方法能夠經過管道傳遞,可是您能夠經過處理文件描述符來解決。 bash

{ { ./aaa.sh | tee bbb.out; } 2>&1 1>&3 | tee ccc.out; } 3>&1 1>&2

另請參見如何grep標準錯誤流(stderr)? 以及什麼時候使用附加的文件描述符? this

在bash(以及ksh和zsh)中,但在其餘POSIX shell(例如破折號)中卻沒有,能夠使用進程替換spa

./aaa.sh > >(tee bbb.out) 2> >(tee ccc.out)

請注意,在bash中,即便仍執行tee命令(ksh和zsh確實等待子./aaa.sh ,該命令也會在./aaa.sh完成後當即返回。 若是您執行./aaa.sh > >(tee bbb.out) 2> >(tee ccc.out); process_logs bbb.out ccc.out這多是一個問題./aaa.sh > >(tee bbb.out) 2> >(tee ccc.out); process_logs bbb.out ccc.out ./aaa.sh > >(tee bbb.out) 2> >(tee ccc.out); process_logs bbb.out ccc.out 。 在這種狀況下,請改用文件描述符雜耍或ksh / zsh。 日誌


#2樓

這對於經過Google找到此信息的人可能有用。 只需取消註釋您要嘗試的示例。 固然,能夠隨意重命名輸出文件。 code

#!/bin/bash

STATUSFILE=x.out
LOGFILE=x.log

### All output to screen
### Do nothing, this is the default


### All Output to one file, nothing to the screen
#exec > ${LOGFILE} 2>&1


### All output to one file and all output to the screen
#exec > >(tee ${LOGFILE}) 2>&1


### All output to one file, STDOUT to the screen
#exec > >(tee -a ${LOGFILE}) 2> >(tee -a ${LOGFILE} >/dev/null)


### All output to one file, STDERR to the screen
### Note you need both of these lines for this to work
#exec 3>&1
#exec > >(tee -a ${LOGFILE} >/dev/null) 2> >(tee -a ${LOGFILE} >&3)


### STDOUT to STATUSFILE, stderr to LOGFILE, nothing to the screen
#exec > ${STATUSFILE} 2>${LOGFILE}


### STDOUT to STATUSFILE, stderr to LOGFILE and all output to the screen
#exec > >(tee ${STATUSFILE}) 2> >(tee ${LOGFILE} >&2)


### STDOUT to STATUSFILE and screen, STDERR to LOGFILE
#exec > >(tee ${STATUSFILE}) 2>${LOGFILE}


### STDOUT to STATUSFILE, STDERR to LOGFILE and screen
#exec > ${STATUSFILE} 2> >(tee ${LOGFILE} >&2)


echo "This is a test"
ls -l sdgshgswogswghthb_this_file_will_not_exist_so_we_get_output_to_stderr_aronkjegralhfaff
ls -l ${0}

#3樓

以我爲例,腳本在將stdout和stderr都重定向到文件時正在運行命令,例如: orm

cmd > log 2>&1

我須要對其進行更新,以便在出現故障時根據錯誤消息採起一些措施。 我固然能夠刪除dup 2>&1並從腳本中捕獲stderr,可是錯誤消息不會進入日誌文件以供參考。 雖然@lhunath的可接受答案應該是相同的,但它會將stdoutstderr重定向到不一樣的文件,這不是我想要的,但它幫助我提出了所需的確切解決方案: htm

(cmd 2> >(tee /dev/stderr)) > log

經過以上操做,日誌將同時包含stdoutstderr的副本,而且我能夠從腳本中捕獲stderr而沒必要擔憂stdout進程


#4樓

如下內容適用於沒法進行進程替換的KornShell(ksh),

# create a combined(stdin and stdout) collector
exec 3 <> combined.log

# stream stderr instead of stdout to tee, while draining all stdout to the collector
./aaa.sh 2>&1 1>&3 | tee -a stderr.log 1>&3

# cleanup collector
exec 3>&-

這裏的實際伎倆,是序列2>&1 1>&3這在咱們的狀況下,重定向stderrstdout和所述重定向stdout到描述符3 。 此時, stderrstdout還沒有合併。

實際上,將stderr (做爲stdin )傳遞到tee ,並在其中記錄到stderr.log並重定向到描述符3。

描述符3一直將其記錄到combined.log 。 所以, combined.log包含stdoutstderr


#5樓

要將stderr重定向到文件,請在屏幕上顯示stdout,並將stdout保存到文件:

./aaa.sh 2>ccc.out | tee ./bbb.out

編輯 :要在屏幕上同時顯示stderr和stdout並將它們保存到文件,能夠使用bash的I / O重定向

#!/bin/bash

# Create a new file descriptor 4, pointed at the file
# which will receive stderr.
exec 4<>ccc.out

# Also print the contents of this file to screen.
tail -f ccc.out &

# Run the command; tee stdout as normal, and send stderr
# to our file descriptor 4.
./aaa.sh 2>&4 | tee bbb.out

# Clean up: Close file descriptor 4 and kill tail -f.
exec 4>&-
kill %1
相關文章
相關標籤/搜索