使用“tee"时如何将 stderr 写入文件?用管子? [英] How do I write stderr to a file while using "tee" with a pipe?

查看:22
本文介绍了使用“tee"时如何将 stderr 写入文件?用管子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何使用teeaaa.sh的输出(STDOUT)写入bbb.out,同时仍然在终端中显示它:

./aaa.sh |发球台 bbb.out

我现在如何将 STDERR 写入名为 ccc.out 的文件,同时仍然显示它?

解决方案

我假设您仍然希望在终端上看到 STDERR 和 STDOUT.您可以选择 Josh Kelley 的答案,但我发现在后台保留一个 tail 输出您的日志文件非常hackish 和笨拙.请注意您需要如何保留一个 exra FD 并在之后通过杀死它来进行清理,并且技术上应该在 trap '...' EXIT 中执行此操作.

有一种更好的方法可以做到这一点,而且您已经发现了它:tee.

只有一个用于标准输出的 T 恤和一个用于标准错误的 T 恤,而不是仅仅用于您的标准输出.你将如何做到这一点?进程替换和文件重定向:

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

让我们分开解释:

>>(..)

>(...)(进程替换)创建一个 FIFO 并让 tee 监听它.然后,它使用 >(文件重定向)将 command 的 STDOUT 重定向到您的第一个 tee 正在侦听的 FIFO.

第二个也是一样:

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

我们再次使用进程替换来创建一个 tee 进程,该进程从 STDIN 读取并将其转储到 stderr.log.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"陷阱 'rm "$out" "$err"' 退出tee -a stdout.log <"$out" &tee -a stderr.log <"$err" >&2 &命令 >"$out" 2>"$err"

I know how to use tee to write the output (STDOUT) of aaa.sh to bbb.out, while still displaying it in the terminal:

./aaa.sh | tee bbb.out

How would I now also write STDERR to a file named ccc.out, while still having it displayed?

解决方案

I'm assuming you want to still see STDERR and STDOUT on the terminal. You could go for Josh Kelley's answer, but I find keeping a tail around in the background which outputs your log file very hackish and cludgy. Notice how you need to keep an exra FD and do cleanup afterward by killing it and technically should be doing that in a trap '...' EXIT.

There is a better way to do this, and you've already discovered it: tee.

Only, instead of just using it for your stdout, have a tee for stdout and one for stderr. How will you accomplish this? Process substitution and file redirection:

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

Let's split it up and explain:

> >(..)

>(...) (process substitution) creates a FIFO and lets tee listen on it. Then, it uses > (file redirection) to redirect the STDOUT of command to the FIFO that your first tee is listening on.

Same thing for the second:

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

We use process substitution again to make a tee process that reads from STDIN and dumps it into stderr.log. tee outputs its input back on STDOUT, but since its input is our STDERR, we want to redirect tee's STDOUT to our STDERR again. Then we use file redirection to redirect command's STDERR to the FIFO's input (tee's STDIN).

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

Process substitution is one of those really lovely things you get as a bonus of choosing bash as your shell as opposed to sh (POSIX or Bourne).


In sh, you'd have to do things manually:

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"

这篇关于使用“tee"时如何将 stderr 写入文件?用管子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆