我的问题与https://serverfault.com/questions/171095/how-do-i-join-two-named-pipes-into-single-input-stream-in-linux有关,但设置稍微复杂一些。
我有三个程序,cmd1,cmd2和cmd3;
cmd1不接受输入并写入stdout。
cmd2读取stdin或给定的文件并写入stdout。
cmd3读取两个文件
这些程序的数据流如下:cmd2使用cmd1生成的数据,cmd3使用cmd1和cmd2生成的数据:
cmd1 ---+-----> cmd2 --->
| cmd3
+---------------> 如何使用>()、管道和tee使用单个命令行实现这个数据流?
我最好的猜测是cmd1 | tee >(cmd2) > >(cmd3)。
发布于 2018-06-22 21:17:16
mkfifo thepipe
cmd3 <( cmd1 | tee thepipe ) <( cmd2 thepipe )这使用一个命名管道thepipe在tee和cmd2之间传输数据。
使用您的图表:
cmd1 ---(tee)---(thepipe)--- cmd2 --->
| cmd3
+-------------------------->例举
cmd1 = echo 'hello world'将字符串写入标准输出。cmd2 = rev,反转每行字符的顺序,读取文件或标准输入。cmd3 = paste,从两个文件(在本例中)获取输入,并生成两列。mkfifo thepipe
paste <( echo 'hello world' | tee thepipe ) <( rev thepipe )结果:
hello world dlrow olleh同样的事情,但是将命名的管道放在图中的另一个分支上:
cmd1 ---(tee)--------------- cmd2 --->
| cmd3
+-----(thepipe)------------>cmd3 thepipe <( cmd1 | tee thepipe | cmd2 )使用我们的示例命令:
paste thepipe <( echo 'hello world' | tee thepipe | rev )这将产生与上面相同的输出。
显然还有其他的可能性,例如
cmd1 | tee >( cmd2 >thepipe ) | cmd3 /dev/stdin thepipe但我认为,除非您乐于将中间结果写入临时文件并将其分解为两组命令,否则您无法避免使用命名管道。
https://unix.stackexchange.com/questions/451362
复制相似问题