使用下一条命令上的管道将STDOUT和STDERR捕获为单独的参数?

时间:2019-06-12 02:52:01

标签: shell pipe stdout stdin stderr

是否可以使用一条命令将STDOUT和STDERR捕获为单独的流,以用作下一条命令中的参数?

就是一个例子。

# foo outputs to stdout and stderr
# bar recieves in -i and -j streams that could be files but in general they are streams, so it would make sence to pipe to any of this two. 
$ foo | bar -i - -j -&2 

我可以用bash这样做吗?或其他外壳?

1 个答案:

答案 0 :(得分:2)

您可以在命名管道的帮助下完成此操作:

mkfifo /tmp/whatever
foo 2>/tmp/whatever | bar -i - -j /tmp/whatever

实际上没有数据写入/tmp/whatever。内核将写入过程的输出直接发送到读取过程。