在管道上使用"tee"时如何将stderr写入文件?

jpa*_*ich 507 unix linux bash

我知道如何使用tee写输出(STDOUT中)aaa.shbbb.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)

  • 对于那些"看到相信"的人来说,快速测试:`(echo"Test Out";>&2 echo"Test Err")>>(tee stdout.log)2 >>(tee stderr.log>&2) (11认同)
  • [哇](https://www.youtube.com/watch?v=YNkUGUY7OQw)。好答案:“让我们把它分开解释”+1 (6认同)
  • 我试过这个:`$ echo"HANG">>(tee stdout.log)2 >>(tee stderr.log>&2)`哪个有效,但等待输入.有这么简单的原因吗? (4认同)
  • 这是否可以用于将两者写入同一个文件,但以同步方式,以便保留 stderr 和 stdout 消息的顺序,同时 subprocess 的每个流仍然定向到调用进程的相应流?我不知道,但我可以想象,如果 tee 做了一些缓冲,子进程可能会完成向 stdout 写入内容并写入 stderr,但接收 stderr 消息的第二个 tee 可能会比第一个更早完成将其写入文件。 (3认同)

小智 637

为什么不简单:

./aaa.sh 2>&1 | tee -a log
Run Code Online (Sandbox Code Playgroud)

这只是简单地重定向stderrstdout,所以T恤回应记录和屏幕.也许我错过了一些东西,因为其他一些解决方案看起来很复杂.

注意:从bash版本4开始,您可以使用|&以下缩写2>&1 |:

./aaa.sh |& tee -a log
Run Code Online (Sandbox Code Playgroud)

  • 如果你想让stdout(通道1)和stderr(通道2)都记录到同一个文件(包含stdout和sterr混合的单个文件),这样可以正常工作.另一个更复杂的解决方案允许您将stdout和stderr分成2个不同的文件(分别是stdout.log和stderr.log).有时这很重要,有时却不重要. (81认同)
  • 在许多情况下,其他解决方案远比必要复杂得多.这个适合我. (17认同)
  • 此方法的问题是您从aaa.sh进程中丢失了退出/状态代码,这可能很重要(例如,在makefile中使用时).您接受的答案没有这个问题. (13认同)
  • 如果你不介意合并stdout/stderr然后`./aaa.sh |&tee aaa.log`工作(在bash中). (9认同)
  • @Stefaan我相信你可以保留退出状态,如果你在命令链前添加`set -o pipefail`后跟`;`或`&&`如果我没有弄错的话. (5认同)
  • 正式在4中添加了`|&`作为“ 2&gt;&1 |`(http://wiki.bash-hackers.org/bash4)的”同义词“。 (2认同)
  • @Stefaan - 这个问题可以使用数组变量 PIPESTATUS 来解决。在您的示例中,退出状态将位于 $PIPESTATUS[0] 中。 (2认同)

小智 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)

  • 不,我猜exec可以使用一些解释.`exec>`表示将文件描述符的目标移动到某个目的地.默认值为1,因此,`exec>/dev/null`在此会话中从现在开始将stdout的输出移动到/ dev/null.通过执行`ls -l/dev/fd /`可以看到此会话的当前文件描述符.试试吧!然后看看当你发出`exec 2>/tmp/stderr.log时会发生什么.另外,`exec 3>&1`意味着,创建一个数字为3的新文件描述符,并将其重定向到文件描述符1的目标.例如,目标是发出命令时的屏幕. (5认同)
  • 在屏幕和单独文件中显示 stdout 和 stderr 的示例非常棒!多谢! (2认同)

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)

  • 我应该更清楚一些,我也确实希望将stderr显示在屏幕上。我仍然很喜欢Josh Kelley的解决方案,但是找到了更适合我需求的百叶窗。多谢你们! (2认同)

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.

  • 这似乎是允许将stdout/stderr流保持原样的唯一答案(例如,不合并它们).甜! (4认同)

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 很好地解释的接受的答案一样,您可以使用

\n
command > >(tee -a stdout.log) 2> >(tee -a stderr.log >&2)\n
Run Code Online (Sandbox Code Playgroud)\n

请注意,如果您使用 bash,可能会遇到一些问题

\n

让我以马修-威尔科克森为例。

\n
\n

对于那些“眼见为实”的人来说,快速测试一下:

\n
(echo "Test Out";>&2 echo "Test Err") > >(tee stdout.log) 2> >(tee stderr.log >&2)\n
Run Code Online (Sandbox Code Playgroud)\n
\n

就我个人而言,当我尝试时,我得到了这样的结果:

\n
(echo "Test Out";>&2 echo "Test Err") > >(tee stdout.log) 2> >(tee stderr.log >&2)\n
Run Code Online (Sandbox Code Playgroud)\n

两条消息不会出现在同一级别。为什么Test Out看起来像是我以前的命令?

\n

提示位于空白行,让我认为该过程尚未完成,当我按此修复Enter它时。\n当我检查文件的内容时,一切正常,并且重定向有效。

\n

让\xe2\x80\x99s 再进行一次测试。

\n
function outerr() {\n  echo "out"     # stdout\n  echo >&2 "err" # stderr\n}\n
Run Code Online (Sandbox Code Playgroud)\n
user@computer:~$ (echo "Test Out";>&2 echo "Test Err") > >(tee stdout.log) 2> >(tee stderr.log >&2)\nuser@computer:~$ Test Out\nTest Err\n\n
Run Code Online (Sandbox Code Playgroud)\n

再次尝试重定向,但使用此功能:

\n
function 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}\n
Run Code Online (Sandbox Code Playgroud)\n

就我个人而言,我有这样的结果:

\n
function outerr() {\n  echo "out"     # stdout\n  echo >&2 "err" # stderr\n}\n
Run Code Online (Sandbox Code Playgroud)\n

空行上没有提示,但我没有看到正常的输出。stdout.log内容似乎错误的,只有stderr.log似乎没问题。

\n

如果我重新启动它,输出可能会有所不同......

\n

所以为什么?

\n

因为,就像这里解释的那样

\n
\n

请注意,在 bash 中,此命令会在 [第一个命令] 完成后立即返回,即使 tee 命令仍在执行(ksh 和 zsh 会等待子进程)

\n
\n

因此,如果您使用 Bash,更喜欢使用另一个答案中给出的更好的示例:

\n
user@computer:~$ outerr\nout\nerr\n\nuser@computer:~$ outerr >/dev/null\nerr\n\nuser@computer:~$ outerr 2>/dev/null\nout\n
Run Code Online (Sandbox Code Playgroud)\n

它将解决以前的问题。

\n

现在的问题是,如何检索退出状态代码?

\n

$?不起作用。

\n

set -o pipefail我没有找到比使用(set +o pipefail关闭)打开pipefail更好的解决方案并${PIPESTATUS[0]}像这样使用:

\n
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}\n
Run Code Online (Sandbox Code Playgroud)\n
function 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}\n
Run Code Online (Sandbox Code Playgroud)\n


Arm*_*ius 6

如果您使用的是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(请参阅重定向)。


har*_*dsv 5

就我而言,脚本在将 stdout 和 stderr 重定向到文件时正在运行命令,例如:

cmd > log 2>&1
Run Code Online (Sandbox Code Playgroud)

我需要更新它,以便在出现故障时,根据错误消息采取一些措施。我当然可以删除 dup2>&1并从脚本中捕获 stderr,但是错误消息不会进入日志文件以供参考。虽然从@lhunath接受的答案是应该做同样的,它重定向stdoutstderr不同的文件,这是不是我想要的,但它让我拿出精确解,我需要:

(cmd 2> >(tee /dev/stderr)) > log
Run Code Online (Sandbox Code Playgroud)

通过上述,日志将同时拥有的副本stdout,并stderr和我可以捕捉stderr从我的脚本,而不必操心stdout


shu*_*uva 5

以下将适用于进程替换不可用的 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在我们的例子中重定向stderrstdout和重定向stdout到描述符的序列3。此时stderrstdout尚未合并。

实际上,stderr(as stdin) 被传递到tee它登录的地方,stderr.log并重定向到描述符 3。

描述符3一直在记录它combined.log。所以combined.log包含stdoutstderr