我如何写错误输出到一个文件,而使用"三通"带管? [英] How do I write stderr to a file while using "tee" with a pipe?

查看:93
本文介绍了我如何写错误输出到一个文件,而使用"三通"带管?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个命令,它会打印 aaa.sh 的输出到屏幕上,同时也写的标准输出 bbb.out ;不过,我还想写的标准错误的一个名为 ccc.out 文件。关于如何修改下面?那块任何建议

I have below a command which will print the output of aaa.sh to the screen while also writing stdout to bbb.out; however I would also like to write stderr to a file named ccc.out. Any suggestions on how to modify the piece below?

./aaa.sh | tee ./bbb.out

更新: 标准输出的和的标准错误的应该还是都被打印在屏幕上,无论

Update: stdout and stderr should still both be printed to the screen, regardless.

推荐答案

我假设你希望仍然可以看到在终端上STDERR和STDOUT。你可以去乔许·凯利的回答,但我觉得在输出日志文件非常hackish的和cludgy背景保持各地。请注意,你需要怎么杀死它之后保持一个关节外FD和做的清理和技术上应该做的事情,在陷阱'...'退出

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.

有一个更好的方式来做到这一点,你已经发现了它: T恤

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 stdout.log) 2> >(tee stderr.log >&2)

让我们把它分解了,并解释:

Let's split it up and explain:

> >(..)

>(...)(进程替换)创建一个FIFO,并让 T恤听就可以了。然后,它使用> (文件重定向)为命令的标准输出重定向到FIFO,你的第一个 T恤正在监听。

>(...) (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.

第二次同样的事情:

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

我们再次使用进程替换作 T恤工艺,从标准输入读取并转储到 stderr.log T恤输出其输入回标准输出,但由于它的输入是我们的STDERR,我们要重定向 T恤的STDOUT我们再次STDERR。然后我们使用文件重定向重定向命令的STDERR到FIFO的输入( T恤的STDIN)。

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).

请参阅 <一个href=\"http://mywiki.wooledge.org/BashGuide/InputAndOutput\">http://mywiki.wooledge.org/BashGuide/InputAndOutput

进程替换是那些真正可爱的东西,你得到尽可能选择的奖金有一个庆典作为你的shell而不是 SH (POSIX或Bourne)。

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).

SH ,你必须手工做的事情:

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 stdout.log < "$out" &
tee stderr.log < "$err" >&2 &
command >"$out" 2>"$err"

这篇关于我如何写错误输出到一个文件,而使用&QUOT;三通&QUOT;带管?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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