简体   繁体   English

将stderr和stdout重定向到Bash中的单独文件?

[英]Redirect stderr and stdout to separate files in Bash?

I am able to redirect stdout and stderr to separate files using: 我可以使用以下命令将stdoutstderr重定向到单独的文件:

dir >> out 2>> error

or stderror and stdout together to a single file using: 使用以下stderrorstdout合并为一个文件:

dir >> consolidate 2>&1

How can I do this together (get out, error, consolidate files at a time)? 我怎么能一起做这个(出去,错误,一次合并文件)?

You can try something like: 您可以尝试以下方式:

(command > >(tee out.txt) 2> >(tee error.txt >&2)) &> consol.txt

Test: 测试:

$ ls
f

$ ls g*
ls: cannot access g*: No such file or directory

$ (ls g f > >(tee out.txt) 2> >(tee error.txt >&2)) &> consol.txt

$ cat out.txt
f

$ cat error.txt
ls: cannot access g: No such file or directory

$ cat consol.txt
f
ls: cannot access g: No such file or directory

不需要任何bashisms,因为这可以很容易地在标准sh中完成:

{ { dir | tee -a out; } 2>&1 >&3 | tee -a error; } >> consolidate 3>&1

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

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