bash - 将两个进程输出管道发送到双管道输入进程

标签 bash redirect pipe

我有两个输出到 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/

相关文章:

bash - 管道 heredoc 的多行语法;这是可移植的吗?

c - 在 C 中从管道写入和读取整数

linux - 在 ssh 进入 DC 的时钟服务器后,如何在我的脚本中触发脚本?

linux - 在 Bash 中导入远程源文件

c - 当您从 execvp 接收输出时,从管道中读取适量的数据

apache - htaccess 使用 OR 和 NC 重定向

java - 转发后是否允许重定向请求?

linux - Minicom 运行脚本不理解变量

bash - 而变量不等于 x 或 y bash

php - HTTP 转发 PLAINTEXT 警告