gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:14:39 26 4
gpt4 key购买 nike

是否可以将输入通过管道传递给两个进程替换?可以用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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com