簡體   English   中英

在管道中使用“tee”時如何將 stderr 寫入文件?

[英]How do I write stderr to a file while using "tee" with a pipe?

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

./aaa.sh | tee bbb.out

我現在如何將STDERR寫入名為ccc.out的文件,同時仍然顯示它?

我假設您仍然希望在終端上看到 STDERR 和 STDOUT。 您可以選擇 Josh Kelley 的答案,但我發現在后台保留一條tail ,輸出您的日志文件非常笨拙和笨拙。 請注意您需要如何保留 exra FD 並在之后通過殺死它來進行清理,並且技術上應該在trap '...' EXIT

有一種更好的方法可以做到這一點,您已經發現了它: tee

只是,而不是僅僅將它用於您的標准輸出,有一個用於標准輸出的 T 恤和一個用於標准錯誤的 T 恤。 你將如何做到這一點? 進程替換和文件重定向:

command > >(tee -a stdout.log) 2> >(tee -a stderr.log >&2)

讓我們把它分開並解釋:

> >(..)

>(...) (進程替換)創建一個 FIFO 並讓tee監聽它。 然后,它使用> (文件重定向)將command的 STDOUT 重定向到您的第一個tee正在偵聽的 FIFO。

第二個同樣的事情:

2> >(tee -a stderr.log >&2)

我們再次使用進程替換來創建一個從 STDIN 讀取並將其轉儲到stderr.logtee進程。 tee在 STDOUT 上輸出它的輸入,但由於它的輸入是我們的 STDERR,我們想再次將tee的 STDOUT 重定向到我們的 STDERR。 然后我們使用文件重定向將command的 STDERR 重定向到 FIFO 的輸入( tee的 STDIN)。

http://mywiki.wooledge.org/BashGuide/InputAndOutput

進程替換是您選擇bash作為 shell 而不是sh (POSIX 或 Bourne)的額外獎勵之一。


sh ,您必須手動執行以下操作:

out="${TMPDIR:-/tmp}/out.$$" err="${TMPDIR:-/tmp}/err.$$"
mkfifo "$out" "$err"
trap 'rm "$out" "$err"' EXIT
tee -a stdout.log < "$out" &
tee -a stderr.log < "$err" >&2 &
command >"$out" 2>"$err"

為什么不簡單:

./aaa.sh 2>&1 | tee -a log

這只是將stderr重定向到stdout ,因此 tee 回顯到日志和屏幕。 也許我遺漏了一些東西,因為其他一些解決方案看起來非常復雜。

注意:從 bash 版本 4 開始,您可以使用|&作為2>&1 |的縮寫。

./aaa.sh |& tee -a log

這對於通過谷歌找到這個的人可能很有用。 只需取消注釋您要嘗試的示例即可。 當然,您可以隨意重命名輸出文件。

#!/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}

要將 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

換句話說,您希望通過管道將 stdout tee bbb.out到一個過濾器 ( tee bbb.out ) 並將 stderr tee bbb.out到另一個過濾器 ( tee ccc.out )。 沒有標准方法可以將 stdout 以外的任何內容通過管道傳輸到另一個命令中,但是您可以通過處理文件描述符來解決這個問題。

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

另請參閱如何 grep 標准錯誤流 (stderr)? 什么時候使用額外的文件描述符?

在 bash(以及 ksh 和 zsh)中,但不是在其他 POSIX shell 中,例如 dash,您可以使用進程替換

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

請注意,在 bash 中,此命令會在./aaa.sh完成后立即返回,即使tee命令仍在執行(ksh 和 zsh 確實會等待子進程)。 如果您執行類似./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 ./aaa.sh > >(tee bbb.out) 2> >(tee ccc.out); process_logs bbb.out ccc.out 在這種情況下,請改用文件描述符雜耍或 ksh/zsh。

如果使用 bash:

# Redirect standard out and standard error separately
% cmd >stdout-redirect 2>stderr-redirect

# Redirect standard error and out together
% cmd >stdout-redirect 2>&1

# Merge standard error with standard out and pipe
% cmd 2>&1 |cmd2

信用(不是從我的頭頂回答)在這里: http : //www.cygwin.com/ml/cygwin/2003-06/msg00772.html

如果您使用的是zsh ,則可以使用多個重定向,因此您甚至不需要tee

./cmd 1>&1 2>&2 1>out_file 2>err_file

在這里,您只需將每個流重定向到自身目標文件。


完整示例

% (echo "out"; echo "err">/dev/stderr) 1>&1 2>&2 1>/tmp/out_file 2>/tmp/err_file
out
err
% cat /tmp/out_file
out
% cat /tmp/err_file
err

請注意,這需要設置MULTIOS選項(這是默認設置)。

MULTIOS

當嘗試多次重定向時,執行隱式tee s 或cat s(請參閱重定向)。

就我而言,腳本在將 stdout 和 stderr 重定向到文件時正在運行命令,例如:

cmd > log 2>&1

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

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

有了上面的內容, log 將擁有stdoutstderr的副本,我可以從我的腳本中捕獲stderr ,而不必擔心stdout

以下將適用於進程替換不可用的 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

就像lhunath很好解釋公認答案一樣,您可以使用

command > >(tee -a stdout.log) 2> >(tee -a stderr.log >&2)

當心,如果您使用 bash,您可能會遇到一些問題

讓我以馬修-威爾科克森為例。

對於那些“眼見為實”的人,快速測試一下:

 (echo "Test Out";>&2 echo "Test Err") > >(tee stdout.log) 2> >(tee stderr.log >&2)

就個人而言,當我嘗試時,我得到了這樣的結果:

user@computer:~$ (echo "Test Out";>&2 echo "Test Err") > >(tee stdout.log) 2> >(tee stderr.log >&2)
user@computer:~$ Test Out
Test Err

兩個消息不會出現在同一級別。 為什么Test Out似乎是我以前的命令?
提示在一個空行上,讓我認為這個過程沒有完成,當我按Enter修復它。
當我檢查文件的內容時,沒關系,重定向有效。

讓我們再做一個測試。

function outerr() {
  echo "out"     # stdout
  echo >&2 "err" # stderr
}

user@computer:~$ outerr
out
err

user@computer:~$ outerr >/dev/null
err

user@computer:~$ outerr 2>/dev/null
out

再次嘗試重定向,但使用此功能。

function test_redirect() {
  fout="stdout.log"
  ferr="stderr.log"
  echo "$ outerr"
  (outerr) > >(tee "$fout") 2> >(tee "$ferr" >&2)
  echo "# $fout content :"
  cat "$fout"
  echo "# $ferr content :"
  cat "$ferr"
}

就個人而言,我有這個結果:

user@computer:~$ test_redirect
$ outerr
# stdout.log content :
out
out
err
# stderr.log content :
err
user@computer:~$

空行沒有提示,但是看不到正常輸出,stdout.log內容好像有錯,只有stderr.log好像沒問題。 如果我重新啟動它,輸出可能會有所不同......

所以為什么 ?

因為,就像這里解釋的那樣

請注意,在 bash 中,此命令會在 [first command] 完成后立即返回,即使 tee 命令仍在執行(ksh 和 zsh 確實等待子進程)

因此,如果您使用 bash,請更喜歡使用其他答案中給出的更好的示例:

{ { outerr | tee "$fout"; } 2>&1 1>&3 | tee "$ferr"; } 3>&1 1>&2

它將解決以前的問題。

現在,問題是,如何檢索退出狀態代碼?
$? 不起作用。
我沒有找到比用set -o pipefailset +o pipefail關閉)打開 pipefail 更好的解決方案,並像這樣使用${PIPESTATUS[0]}

function outerr() {
  echo "out"
  echo >&2 "err"
  return 11
}

function test_outerr() {
  local - # To preserve set option
  ! [[ -o pipefail ]] && set -o pipefail; # Or use second part directly
  local fout="stdout.log"
  local ferr="stderr.log"
  echo "$ outerr"
  { { outerr | tee "$fout"; } 2>&1 1>&3 | tee "$ferr"; } 3>&1 1>&2
  # First save the status or it will be lost
  local status="${PIPESTATUS[0]}" # Save first, the second is 0, perhaps tee status code.
  echo "==="
  echo "# $fout content :"
  echo "<==="
  cat "$fout"
  echo "===>"
  echo "# $ferr content :"
  echo "<==="
  cat "$ferr"
  echo "===>"
  if (( status > 0 )); then
    echo "Fail $status > 0"
    return "$status" # or whatever
  fi
}
user@computer:~$ test_outerr
$ outerr
err
out
===
# stdout.log content :
<===
out
===>
# stderr.log content :
<===
err
===>
Fail 11 > 0

您可以使用:

bash: $ gcc temp.c &> error.log

csh: % gcc temp.c |& tee error.log

請參閱: https : //www.linuxquestions.org/questions/linux-kernel-70/how-to-redirect-compilation-build-error-to-a-file-831099/

感謝@lhunath 在 posix 中的回答,這是我在 posix 中需要的一個更復雜的情況,並進行了適當的修復:

# Start script main() function
# - We redirect stdout to file_out AND terminal
# - We redirect stderr to file_err, file_out AND terminal
# - Terminal and file_out have both stdout and stderr, while file_err only holds stderr

main() {
    # my main function
}

log_path="/my_temp_dir"
pfout_fifo="${log_path:-/tmp}/pfout_fifo.$$"
pferr_fifo="${log_path:-/tmp}/pferr_fifo.$$"

mkfifo "$pfout_fifo" "$pferr_fifo"
trap 'rm "$pfout_fifo" "$pferr_fifo"' EXIT

tee -a "file_out" < "$pfout_fifo" &
    tee -a "file_err" < "$pferr_fifo" >>"$pfout_fifo" &
    main "$@" >"$pfout_fifo" 2>"$pferr_fifo"; exit

暫無
暫無

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

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