bash 将 stdout/stderr 重定向到多个文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8124220/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 01:03:53  来源:igfitidea点击:

Redirecting stdout/stderr to multiple files

bashunixstdoutstderr

提问by kevin948

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.

我想知道如何将 stderr 重定向到多个输出。我用这个脚本尝试过,但我无法让它正常工作。第一个文件应该有 stdout 和 stderr,第二个应该只有错误。

perl script.pl &> errorTestnormal.out &2> errorTest.out

Is there a better way to do this? Any help would be much appreciated. Thank you.

有一个更好的方法吗?任何帮助将非常感激。谢谢你。

回答by Jarek

perl script.pl 2>&1 >errorTestnormal.out | tee -a errorTestnormal.out > errorTest.out

perl script.pl 2>&1 >errorTestnormal.out | tee -a errorTestnormal.out > errorTest.out

Will do what you want.

会做你想做的。

This is a bit messy, lets go through it step by step.

这有点乱,让我们一步一步来。

  • We say what used to go to STDERRwill now go STDOUT
  • We say what used to go to STDOUTwill now go to errorTestnormal.out.
  • 我们说曾经去的STDERR现在会去STDOUT
  • 我们说以前去的STDOUT现在会去 errorTestnormal.out。

So now, STDOUTgets printed to a file, and STDERRgets printed to STDOUT. We want put STDERRinto 2 different files, which we can do with tee. tee appends the text it's given to a file, and also echoes to STDOUT.

所以现在,STDOUT被打印到一个文件,然后STDERR被打印到STDOUT. 我们想放入STDERR2 个不同的文件,我们可以用 tee 来做。tee 将它给定的文本附加到文件中,并且还回显到STDOUT.

  • We use teeto append to errorTestnormal.out, so it now contains all the STDOUTand STDERRoutput of script.pl.
  • Then, we write STDOUTof tee(which contains STDERRfrom script.pl) into errorTest.out
  • 我们使用tee附加到errorTestnormal.out,因此它现在包含 的所有STDOUTSTDERR输出script.pl
  • 然后,我们将STDOUTof tee(包含STDERRfrom script.pl) 写入errorTest.out

After this, errorTestnormal.outhas all the STDOUToutput, and then all the STDERRoutput. errotTest.outcontains only the STDERRoutput.

在此之后,errorTestnormal.out拥有所有STDOUT输出,然后是所有STDERR输出。errotTest.out只包含STDERR输出。

回答by R.J.

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:

我也不得不为此纠结了一段时间。为了在两个文件中都获得 stderr,同时只将 stdout 放入单个文件中(例如,stderr 放入 errors.log 和 output.log,然后 stdout 放入 output.log)并且按照它们发生的顺序,这个命令更好:

((sh test.sh 2>&1 1>&3 | tee errors.log) 3>&1 | tee output.log) > /dev/null 2>&1

The last /dev/nul 2>&1 can be omitted if you want the stdout and stderr to still be output onto the screen.

如果您希望 stdout 和 stderr 仍然输出到屏幕上,则可以省略最后一个 /dev/nul 2>&1 。

回答by kol

I guess in case of the 2nd ">" you try to send the error output of errorTestnormal.out(and not that of script.pl) to errorTest.out.

我想在第二个“>”的情况下,您尝试将errorTestnormal.out(而不是script.pl)的错误输出发送到errorTest.out.