将 stdout/stderr 重定向到多个文件
我想知道如何将 stderr 重定向到多个输出。我用这个脚本尝试过,但无法让它正常工作。第一个文件应该有 stdout 和 stderr,第二个文件应该只有错误。
perl script.pl &> errorTestnormal.out &2> errorTest.out
有更好的方法吗?任何帮助将不胜感激。谢谢。
I was wondering how to redirect stderr to multiple outputs. I tried it with this script, but I couldn't get it to work quite right. The first file should have both stdout and stderr, and the 2nd should just have errors.
perl script.pl &> errorTestnormal.out &2> errorTest.out
Is there a better way to do this? Any help would be much appreciated. Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
下面的命令将执行您想要的操作。
这有点混乱,所以让我们一步一步来。
STDERR
的内容现在将转到STDOUT
。STDOUT
的内容现在将转到errorTestnormal.out
。现在,
STDOUT
被打印到文件,STDERR
被打印到STDOUT
。我们希望将STDERR
放入 2 个不同的文件中,我们可以使用 tee 来做到这一点。 tee 将其提供的文本附加到文件中,并且还回显到STDOUT
。tee
附加到errorTestnormal.out
,因此它现在包含STDOUT
和STDERR
输出代码> script.pl 。tee
的STDOUT
(包含script.pl
中的STDERR
)写入errorTest。 out
此后,
errorTestnormal.out
具有所有STDOUT
输出,然后是所有STDERR
输出。errotTest.out
仅包含STDERR
输出。The command below will do what you want.
This is a bit messy, so lets go through it step by step.
STDERR
will now goSTDOUT
.STDOUT
will now go toerrorTestnormal.out
.So now,
STDOUT
gets printed to a file, andSTDERR
gets printed toSTDOUT
. We want putSTDERR
into 2 different files, which we can do with tee. tee appends the text it's given to a file, and also echoes toSTDOUT
.tee
to append toerrorTestnormal.out
, so it now contains all theSTDOUT
andSTDERR
output ofscript.pl
.STDOUT
oftee
(which containsSTDERR
fromscript.pl
) intoerrorTest.out
After this,
errorTestnormal.out
has all theSTDOUT
output, and then all theSTDERR
output.errotTest.out
contains only theSTDERR
output.我也不得不为此折腾了一段时间。为了在两个文件中获取 stderr,同时仅将 stdout 放入单个文件中(例如,将 stderr 放入 error.log 和 output.log,然后将 stdout 放入 output.log)并且按照它们发生的顺序,此命令更好:
如果您希望 stdout 和 stderr 仍输出到屏幕上,则可以省略最后一个 /dev/nul 2>&1 。
I had to mess around with this for a while as well. In order to get stderr in both files, while only putting stdout into a single file (e.g. stderr into errors.log and output.log and then stdout into just output.log) AND in the order that they happen, this command is better:
The last /dev/nul 2>&1 can be omitted if you want the stdout and stderr to still be output onto the screen.
我想如果是第二个“>”您尝试将
errorTestnormal.out
的错误输出(而不是script.pl
)发送到errorTest.out
。I guess in case of the 2nd ">" you try to send the error output of
errorTestnormal.out
(and not that ofscript.pl
) toerrorTest.out
.