我有两个输出到 stdout (fd 1) 的命令(命令 1 和 command2),我想将它们发送到一个新的 command3,它准备好在两个管道中接收它们,一个在来自 command1 的 stdin 和other 在任何其他文件描述符中,即在 fd 3 中,来自 command2。 我如何在 bash 中执行此操作?
最佳答案
这可以通过使用 process subsitution 来完成技术,来自 bash 引用:
Process substitution allows a process’s input or output to be referred to using a filename. It takes the form of
<(list)
or
>(list)
The process list is run asynchronously, and its input or output appears as a filename.
基本上,使用这种技术,您可以像从文件中读取一样读取命令(上例中的列表)的输出。事实上,您可以有几个输入可以解决您的问题,如下所示:
command3 <( command1 ) <( command2 )
为此,您必须打开两个文件(作为参数接收)并从中读取。
进程替换基本上创建一个文件 (/dev/fd/XX) 并将其名称用作接收命令(上例中的 command3)的输入。请记住,命令 command1 和 command2 都将异步运行,因此您在启动上述命令时不能期望/依赖任何执行顺序。
关于bash - 将两个进程输出管道发送到双管道输入进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50795731/