我知道如何使用tee写输出(STDOUT中)aaa.sh到bbb.out,同时还在终端显示它:
./aaa.sh | tee bbb.out
Run Code Online (Sandbox Code Playgroud)
我现在如何写STDERR一个名为的文件ccc.out,同时还显示它?
lhu*_*ath 744
我假设您仍希望在终端上看到STDERR和STDOUT.你可以去找Josh Kelley的答案,但我发现tail在后台保持一个输出你的日志文件非常hackish和cludgy.注意你需要保留一个exra FD并在之后通过杀死它进行清理,技术上应该在一个trap '...' EXIT.
有一种更好的方法可以做到这一点,你已经发现了它:tee.
只是,而不是仅仅为stdout使用它,有stdout的发球台和stderr的发球台.你将如何实现这一目标?进程替换和文件重定向:
command > >(tee -a stdout.log) 2> >(tee -a stderr.log >&2)
Run Code Online (Sandbox Code Playgroud)
让我们分开并解释一下:
> >(..)
Run Code Online (Sandbox Code Playgroud)
>(...)(进程替换)创建一个FIFO并tee让它监听.然后,它使用>(文件重定向)将STDOUT重定向command到您第一个tee正在侦听的FIFO .
同样的事情是第二个:
2> >(tee -a stderr.log >&2)
Run Code Online (Sandbox Code Playgroud)
我们再次使用进程替换来创建一个tee从STDIN读取并将其转储到的进程stderr.log. tee在STDOUT上输出其输入,但由于它的输入是我们的STDERR,我们想要再次将teeSTDOUT 重定向到我们的STDERR.然后我们使用文件重定向将commandSTDERR 重定向到FIFO的输入(tee's 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"
Run Code Online (Sandbox Code Playgroud)
小智 637
为什么不简单:
./aaa.sh 2>&1 | tee -a log
Run Code Online (Sandbox Code Playgroud)
这只是简单地重定向stderr到stdout,所以T恤回应记录和屏幕.也许我错过了一些东西,因为其他一些解决方案看起来很复杂.
注意:从bash版本4开始,您可以使用|&以下缩写2>&1 |:
./aaa.sh |& tee -a log
Run Code Online (Sandbox Code Playgroud)
小智 56
这可能对通过谷歌找到这个的人有用.只需取消注释您想要尝试的示例即可.当然,随意重命名输出文件.
#!/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}
Run Code Online (Sandbox Code Playgroud)
Jos*_*ley 21
要将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
Run Code Online (Sandbox Code Playgroud)
Gil*_*il' 16
换句话说,您希望将stdout传递给一个filter(tee bbb.out)和stderr到另一个filter(tee ccc.out).没有标准方法可以将除stdout之外的任何东西传递给另一个命令,但是你可以通过处理文件描述符来解决这个问题.
{ { ./aaa.sh | tee bbb.out; } 2>&1 1>&3 | tee ccc.out; } 3>&1 1>&2
Run Code Online (Sandbox Code Playgroud)
另请参见如何grep标准错误流(stderr)?和你什么时候会使用一个额外的文件描述符?
在bash(和ksh和zsh)中,但不在其他POSIX shell(如dash)中,您可以使用进程替换:
./aaa.sh > >(tee bbb.out) 2> >(tee ccc.out)
Run Code Online (Sandbox Code Playgroud)
请注意,在bash中,./aaa.sh即使tee命令仍在执行(ksh和zsh等待子进程),此命令也会在完成后立即返回.如果你这样做,这可能是一个问题./aaa.sh > >(tee bbb.out) 2> >(tee ccc.out); process_logs bbb.out ccc.out.在这种情况下,请改用文件描述符juggling或ksh/zsh.
Chr*_*heD 13
如果使用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
Run Code Online (Sandbox Code Playgroud)
信用(不是从我的头脑回答)到这里:http://www.cygwin.com/ml/cygwin/2003-06/msg00772.html
小智 10
就像lhunath 很好地解释的接受的答案一样,您可以使用
\ncommand > >(tee -a stdout.log) 2> >(tee -a stderr.log >&2)\nRun Code Online (Sandbox Code Playgroud)\n请注意,如果您使用 bash,可能会遇到一些问题。
\n让我以马修-威尔科克森为例。
\n\n\n对于那些“眼见为实”的人来说,快速测试一下:
\nRun Code Online (Sandbox Code Playgroud)\n(echo "Test Out";>&2 echo "Test Err") > >(tee stdout.log) 2> >(tee stderr.log >&2)\n
就我个人而言,当我尝试时,我得到了这样的结果:
\n(echo "Test Out";>&2 echo "Test Err") > >(tee stdout.log) 2> >(tee stderr.log >&2)\nRun Code Online (Sandbox Code Playgroud)\n两条消息不会出现在同一级别。为什么Test Out看起来像是我以前的命令?
提示位于空白行,让我认为该过程尚未完成,当我按此修复Enter它时。\n当我检查文件的内容时,一切正常,并且重定向有效。
\n让\xe2\x80\x99s 再进行一次测试。
\nfunction outerr() {\n echo "out" # stdout\n echo >&2 "err" # stderr\n}\nRun Code Online (Sandbox Code Playgroud)\nuser@computer:~$ (echo "Test Out";>&2 echo "Test Err") > >(tee stdout.log) 2> >(tee stderr.log >&2)\nuser@computer:~$ Test Out\nTest Err\n\nRun Code Online (Sandbox Code Playgroud)\n再次尝试重定向,但使用此功能:
\nfunction test_redirect() {\n fout="stdout.log"\n ferr="stderr.log"\n echo "$ outerr"\n (outerr) > >(tee "$fout") 2> >(tee "$ferr" >&2)\n echo "# $fout content: "\n cat "$fout"\n echo "# $ferr content: "\n cat "$ferr"\n}\nRun Code Online (Sandbox Code Playgroud)\n就我个人而言,我有这样的结果:
\nfunction outerr() {\n echo "out" # stdout\n echo >&2 "err" # stderr\n}\nRun Code Online (Sandbox Code Playgroud)\n空行上没有提示,但我没有看到正常的输出。stdout.log内容似乎是错误的,只有stderr.log似乎没问题。
\n如果我重新启动它,输出可能会有所不同......
\n所以为什么?
\n因为,就像这里解释的那样:
\n\n\n请注意,在 bash 中,此命令会在 [第一个命令] 完成后立即返回,即使 tee 命令仍在执行(ksh 和 zsh 会等待子进程)
\n
因此,如果您使用 Bash,更喜欢使用另一个答案中给出的更好的示例:
\nuser@computer:~$ outerr\nout\nerr\n\nuser@computer:~$ outerr >/dev/null\nerr\n\nuser@computer:~$ outerr 2>/dev/null\nout\nRun Code Online (Sandbox Code Playgroud)\n它将解决以前的问题。
\n现在的问题是,如何检索退出状态代码?
\n$?不起作用。
set -o pipefail我没有找到比使用(set +o pipefail关闭)打开pipefail更好的解决方案并${PIPESTATUS[0]}像这样使用:
function outerr() {\n echo "out"\n echo >&2 "err"\n return 11\n}\n\nfunction test_outerr() {\n local - # To preserve set option\n ! [[ -o pipefail ]] && set -o pipefail; # Or use second part directly\n local fout="stdout.log"\n local ferr="stderr.log"\n echo "$ outerr"\n { { outerr | tee "$fout"; } 2>&1 1>&3 | tee "$ferr"; } 3>&1 1>&2\n # First save the status or it will be lost\n local status="${PIPESTATUS[0]}" # Save first, the second is 0, perhaps tee status code.\n echo "==="\n echo "# $fout content :"\n echo "<==="\n cat "$fout"\n echo "===>"\n echo "# $ferr content :"\n echo "<==="\n cat "$ferr"\n echo "===>"\n if (( status > 0 )); then\n echo "Fail $status > 0"\n return "$status" # or whatever\n fi\n}\nRun Code Online (Sandbox Code Playgroud)\nfunction test_redirect() {\n fout="stdout.log"\n ferr="stderr.log"\n echo "$ outerr"\n (outerr) > >(tee "$fout") 2> >(tee "$ferr" >&2)\n echo "# $fout content: "\n cat "$fout"\n echo "# $ferr content: "\n cat "$ferr"\n}\nRun Code Online (Sandbox Code Playgroud)\n
如果您使用的是zsh,则可以使用多个重定向,因此您甚至不需要tee:
./cmd 1>&1 2>&2 1>out_file 2>err_file
Run Code Online (Sandbox Code Playgroud)
在这里,您只需将每个流重定向到自身和目标文件。
完整示例
% (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
Run Code Online (Sandbox Code Playgroud)
请注意,这需要设置MULTIOS选项(这是默认设置)。
MULTIOS尝试多次重定向时执行隐式
tees 或cats(请参阅重定向)。
就我而言,脚本在将 stdout 和 stderr 重定向到文件时正在运行命令,例如:
cmd > log 2>&1
Run Code Online (Sandbox Code Playgroud)
我需要更新它,以便在出现故障时,根据错误消息采取一些措施。我当然可以删除 dup2>&1并从脚本中捕获 stderr,但是错误消息不会进入日志文件以供参考。虽然从@lhunath接受的答案是应该做同样的,它重定向stdout和stderr不同的文件,这是不是我想要的,但它让我拿出精确解,我需要:
(cmd 2> >(tee /dev/stderr)) > log
Run Code Online (Sandbox Code Playgroud)
通过上述,日志将同时拥有的副本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>&-
Run Code Online (Sandbox Code Playgroud)
这里真正的技巧是2>&1 1>&3在我们的例子中重定向stderr到stdout和重定向stdout到描述符的序列3。此时stderr和stdout尚未合并。
实际上,stderr(as stdin) 被传递到tee它登录的地方,stderr.log并重定向到描述符 3。
描述符3一直在记录它combined.log。所以combined.log包含stdout和stderr。