[英]How do I write stderr to a file while using "tee" with a pipe?
我知道如何使用tee
将aaa.sh
的输出( STDOUT
) aaa.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.log
的tee
进程。 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 接受的答案应该做同样的事情,但它将stdout
和stderr
重定向到不同的文件,这不是我想要的,但它帮助我想出了我需要的确切解决方案:
(cmd 2> >(tee /dev/stderr)) > log
有了上面的内容, log 将拥有stdout
和stderr
的副本,我可以从我的脚本中捕获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
这在我们的情况下,重定向stderr
到stdout
和所述重定向stdout
到描述符3
。 此时stderr
和stdout
尚未合并。
实际上, stderr
(作为stdin
)被传递到tee
,它记录到stderr.log
并重定向到描述符 3。
描述符3
一直将其记录到combined.log
。 所以combined.log
包含stdout
和stderr
。
就像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 pipefail
( set +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.