我知道如何重定向到文件,并使用tee;在基本水平上。所以
$ alias outanderr='bash -c "echo stdout >&1; echo stderr >&2"'
# A fake "application" displaying both output and error messages.
$ outanderr 1>file # redirect stdout to a file, display stderr
stderr
$ outanderr 2>file # redirect stderr to a file, display stdout
stdout
$ outanderr 1>file 2>&1 # redirect both to a file, display nothing
$ outanderr | tee file; echo "-- file contents --" && cat file
# redirect stdout to a file, display both (note: order is messed up)
stderr
stdout
-- file contents --
stdout
$ outanderr 2>&1 | tee file; echo "-- file contents --" && cat file
# redirect both to a file, display both
stdout
stderr
-- file contents --
stdout
stderr
问题是:用什么代替问号可以得到下面的输出:
$ outanderr ???; echo "-- file contents --" && cat file
# redirect both to a file, display stderr
stderr
-- file contents --
stdout
stderr
内容:
2>&1 >>outputfile | tee --append outputfile
为了方便测试:
echo -n >outputfile; bash -c "echo stdout >&1; echo stderr >&2" 2>&1 >>outputfile |
tee --append outputfile; echo "outputfile:"; cat outputfile
编辑1:
这是通过(仅)将stdout写入文件,使sterr stdout使其通过管道并让tee将其输出写入同一文件来实现的。
两次写入都必须在附加模式下(>>
而不是>
)完成,否则两者都将覆盖彼此的输出。
由于管道是缓冲区,因此无法保证输出以正确的顺序出现在文件中。如果将应用程序连接到两个文件描述符(两个管道),这甚至都不会改变。为了保证顺序,两个输出都必须经过相同的通道并分别进行标记。否则,您将需要一些真正有趣的东西:
/dev/null
。通过运行该应用程序,可以将其输出分开strace -f -s 32000 -e trace=write
。在这种情况下,您将不得不转义。不用说,通过跟踪应用程序不会运行得更快。本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句