linux - 使用 tee 将输入管道输入到两个进程替换

标签 linux command-line pipe tee process-substitution

是否可以将输入通过管道传递给两个进程替换? 可以用tee来做吗?还没找到解决办法。

我有一个命令,我想使用这样的进程替换来运行:

cat input.txt | command arg1 arg2 <(command2 </dev/stdin) arg3 <(command3 </dev/stdin) arg4

我试图通过管道将输入传递给 command2 和 command3,但我发现您无法从管道读取两次。

如果可能的话,使用 tee 执行此操作的正确语法是什么?

最佳答案

也许你已经减少了你的例子太多,但你可以这样做:

cat input.txt | command arg1 arg2 <(command2 input.txt) arg3 <(command3 input.txt) arg4

或者没有猫。

<input.txt command arg1 arg2 <(command2 input.txt) arg3 <(command3 input.txt) arg4

但也许在管道之前有一个非常复杂的命令。尽管如此,为什么不先将其保存在文件中并执行上述操作呢?

不过,也许输出非常大,您不想将其写入文件系统。然后,您可以使用命名管道,但有一个问题。

mkfifo input{1..3} # makes input1, input2, input3 as named pipes (!! named are still part of the file system !!)
complexcommand | tee input{1..3} & # tee will hang till it can send its output, therefor move it to the background with &
<input1 command arg1 arg2 <(command2 <input2) arg3 <(command3 <input3) arg4
rm -f input{1..3} # Since named pipes are part of the filesystem, better cleanup.

要点:以上内容可能有效,也可能无效,具体取决于命令的行为。它仅在 command、command2 和 command3 同时处理数据时有效。因此,在这种情况下,如果“命令”决定它需要来自 <(command2 <input2) 的所有数据在从 <input1 读取任何数据之前,它将永远等待,因为只有在 command、command2 和 command3 请求时才会发送一行。

关于linux - 使用 tee 将输入管道输入到两个进程替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54715356/

相关文章:

c - 两个 child 执行 ls -la | grep 使用管道

c++ - 管道以提供文件作为 C 程序的输入

linux - 当我运行 test.sh 文件时,Jenkins 控制台显示权限被拒绝错误

c - Linux 中 fork() 调用的来源在哪里?

command-line - 使用 --docopt 解释命令和位置参数

windows - 使用 forfiles 和 7zip 的文件名中的空格

linux - 无法在 Linux 中打印长度超过 8 个字符的用户名( "w")

android - Windows 和 Linux 上的 Android SDK Platforms 文件夹中的文件是否相同

linux - Linux 的排序命令如何将后续传递应用于数据?

bash - 如何按大小对 bash 中的 'find' 结果进行排序