[英]Redirecting program output
How do I redirect my program, so that the output goes to 3 file such that 如何重定向我的程序,以便输出转到3个文件
I tried 我试过了
myprogram > file1 2> file2
but this does not satisfy 3 & 4. 但这不满足3和4。
Edit: It would be better if the screen displays messages immediately after they are printed. 编辑:如果屏幕在打印后立即显示消息会更好。 (to increase responsiveness)
(提高响应能力)
(./foo.sh > >(tee out.log) 2> >(tee err.log >&2)) |& tee all.log
What have we done here? 我们在这做了什么? First, we create two subshells to run
tee out.log
and tee err.log
, and redirect the appropriate descriptors to them. 首先,我们创建两个子shell来运行
tee out.log
和tee err.log
,并将适当的描述符重定向到它们。 We are careful to redirect stdout
from err.log
back to stderr
where it belongs, otherwise it will mess up out.log
(credit to https://stackoverflow.com/a/692407/4323 for this idea). 我们小心地将
stdout
从err.log
重定向回到它所属的stderr
,否则它会搞砸out.log
(这个想法归功于https://stackoverflow.com/a/692407/4323 )。 Second, we put that entire thing in a subshell so that we can redirect its stdout
and stderr
in one shot to all.log
, again using tee
to print to the screen at the same time. 其次,我们将整个事情放在子shell中,以便我们可以将其
stdout
和stderr
一次性重定向到all.log
,再次使用tee
同时打印到屏幕。
One caveat is that the program we're running is likely to buffer stdout when it is not a TTY/PTY (terminal device). 需要注意的是,当我们运行的程序不是TTY / PTY(终端设备)时,它可能会缓冲stdout。 If you need immediate output from
stdout
on your screen and in the files, you can try running your program with unbuffer
, a utility which avoids this buffering. 如果您需要在屏幕上和文件中立即输出
stdout
,您可以尝试使用unbuffer
运行程序,这是一个避免此缓冲的实用程序。
myprogram > file1 2> file2 &> file3; cat file3
或者你认为cat file3
是作弊?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.