管道与重定向到流程 [英] Pipe vs redirect into process
问题描述
寻找一些 bash-expert 的解释.下一个之间的确切区别是什么
Looking for some bash-expert explanation. What is the exact difference between of the next
command1 | command2
例如经典管道,其中command1的标准输出重定向到command2的标准输入,例如
e.g. classic pipe where the stdout of command1 is redirected to the stdin of command2, e.g.
- bash自身分叉两次
- 更改文件描述符
- 执行comman1和command2
和
command1 > >(command2)
结果(和bash动作)相同...
where the result (and the bash actions) are the same...
至少我从
find . -print0 | xargs -0 -n1 -I% echo =%=
和
find . -print0 > >(xargs -0 -n1 -I% echo =%=)
是>>(命令)
只能说" |
"吗?还是不,我错过了什么?
Is the > >(command)
only the longer way saying |
? Or not, and I missed something?
推荐答案
要实现
command1 | command2
外壳程序在父进程中创建一个管道,并将其一端连接到command1的输出(fd 1;它使用dup或dup2),另一端连接到command2的输入(fd 0).
the shell creates a pipe in the parent process and attaches one end of it to the output (fd 1; it uses dup or dup2) of command1, and the other end to the input (fd 0) of command2.
要实现
command1 > >(command 2)
,shell将创建一个FIFO.它将command2的stdin附加到FIFO(通常使用O_WRONLY标志打开),并将FIFO的名称作为位置参数传递给command1.您可以使用 echo>(true)
轻松地看到这一点.
, the shell creates a FIFO. It attaches the command2's stdin to the FIFO (with open using the O_WRONLY flag, usually) and it passes the name of the FIFO as a positional argument to command1. You can easily see this by using echo >(true)
.
如果使用
> >(foo)
,则这些形式确实非常相似.但是,过程替换机制更强大.例如,您可以执行以下操作:
these forms are indeed very similar. However, the process subsititution mechanism is more powerful. For example you get do this kind of thing:
diff -u <(curl 'http://www.a.example.com/') <(curl 'http://www.b.example.com/')
您不能使用管道执行此操作-您不能有两个标准输入.
You can't do that with pipes - you can't have two standard inputs.
这篇关于管道与重定向到流程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!