Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 将stdout/stderr重定向到多个文件_Bash_Unix_Stdout_Stderr - Fatal编程技术网

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

Bash 将stdout/stderr重定向到多个文件,bash,unix,stdout,stderr,Bash,Unix,Stdout,Stderr,我想知道如何将stderr重定向到多个输出。我用这个脚本试过了,但没能让它正常工作。第一个文件应该有stdout和stderr,第二个文件应该只有错误 perl script.pl &> errorTestnormal.out &2> errorTest.out 有更好的方法吗?任何帮助都将不胜感激。谢谢。我猜在第二个“>的情况下,您尝试将errorTestnormal.out(而不是script.pl)的错误输出发送到errorTest.outperl scrip

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

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

有更好的方法吗?任何帮助都将不胜感激。谢谢。

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

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

我会做你想做的

这有点乱,让我们一步一步地看

  • 我们说以前转到
    STDERR
    的内容现在将转到
    STDOUT
  • 我们说过去转到标准输出的内容现在将转到errorTestnormal.out
现在,
STDOUT
被打印到文件中,
STDERR
被打印到
STDOUT
。我们希望将
STDERR
放入两个不同的文件中,这可以通过tee实现。tee将其提供的文本附加到文件,并回显到
STDOUT

  • 我们使用
    tee
    附加到
    errorTestnormal.out
    ,因此它现在包含
    script.pl
    的所有
    STDOUT
    STDERR
    输出
  • 然后,我们将
    tee
    STDOUT
    (其中包含
    script.pl
    中的
    STDERR
    )写入
    errorTest.out

在此之后,
errorTestnormal.out
具有所有的
STDOUT
输出,然后是所有的
STDERR
输出
errotTest.out
仅包含
STDERR
输出

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

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

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

将鼠标悬停在已添加的标记上,stdout/stderr的跟随者总数=7。如果您为正在使用的shell添加一个标记bash,我想您会更加关注您的问题吧?祝你好运。你是一位学者和绅士。非常好的回答,非常感谢:)没有必要使用FD3。您只需执行以下操作:perl script.pl 2>&1>errorTestnormal.out | tee。。。第一个重定向将fd2(stderr)与管道关联,第二个重定向将stdout重定向到文件。如果执行:perl script.pl>errorTestnormal.out 2>&1,则stderr和stdout都将转到该文件。