繁体   English   中英

如何将stderr和stdout重定向到多个输出文件?

[英]How to redirect stderr and stdout to more than one output file?

我想知道如何为以下命令编写3个命令:

  • 将输出从stdout重定向到文件output.txt
  • 将输出从stderr重定向到文件error.txt
  • 将输出从stdout和stderr重定向到文件all.txt

那是我到目前为止的代码,但是没有输出任何内容:

 #!/bin/bash
    echo This goes to stdout
    echo And this is and error going to stderr 1>&2
    exec 1>output.txt
    exec 2>error.txt
    exec >all.txt 1>&2

您可以结合使用tee命令和进程替换来完成

#!/bin/bash

exec 3> all.txt # fd3 goes to all.txt
exec 1> >(tee output.txt >&3) # fd1(stdout) goes to both output.txt and fd3
exec 2> >(tee error.txt >&3) # fd2(stderr) goes to both error.txt and fd3

echo Go To Stdout # goes to fd1, and fd1 goes to both output.txt and fd3 (which goes to all.txt)
echo Go To Stderr >&2 # goes to fd2, and fd2 goes to both error.txt and fd3 (which goes to all.txt)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM