这个Bash(和/或其他shell?)构造叫做什么?

时间:2022-04-21 00:07:30

What is the construct in bash called where you can take wrap a command that outputs to stdout, such that the output itself is treated like a stream? In case I'm not describing that so well, maybe an example will do best, and this is what I typically use it for: applying diff to output that does not come from a file, but from other commands, where

什么是bash中的构造调用,你可以在其中包装一个输出到stdout的命令,这样输出本身被视为一个流?如果我没有那么好地描述,也许一个例子会做得最好,这就是我通常用它来做的:将diff应用于不是来自文件的输出,而是来自其他命令,其中

cmd 

is wrapped as

被包裹成

<(cmd)

By wrapping a command in such a manner, in the example below I determine that there a difference of one between the two commands that I am running, and then I am able to determine that one precise difference. What is the construct/technique of wrapping a command as <(cmd) called? Thanks

通过以这种方式包装命令,在下面的示例中,我确定我正在运行的两个命令之间存在一个差异,然后我能够确定一个精确的差异。将命令包装为<(cmd)调用的构造/技术是什么?谢谢

[builder@george v6.5 html]$ git status | egrep modified | awk '{print $3}' | wc -l
51
[builder@george v6.5 html]$ git status | egrep modified | awk '{print $3}' | xargs grep -l 'Ext\.define' | wc -l
50
[builder@george v6.5 html]$ diff <(git status | egrep modified | awk '{print $3}') <(git status | egrep modified | awk '{print $3}' | xargs grep -l 'Ext\.define')
39d38
< javascript/reports/report_initiator.js

ADDENDUM The revised command using the advice for using git's ls-file should be as follows (untested):

ADDENDUM使用git的ls文件建议的修订版命令应如下(未经测试):

diff <(git ls-files -m) <(git ls-files -m | xargs grep -l 'Ext\.define')

3 个解决方案

#1


6  

It is called process substitution.

它被称为过程替换。

#2


6  

This is called Process Substitution

这称为过程替换

#3


1  

This is process substitution, as you have been told. I'd just like to point out that this also works in the other direction. Process substitution with >(cmd) allows you to take a command that writes to a file and instead have that output redirected to another command's stdin. It's very useful for inserting something into a pipeline that takes an output filename as an argument. You don't see it as much because pretty much every standard command will write to stdout already, but I have used it often with custom stuff. Here is a contrived example:

正如您所知,这是流程替代。我只想指出,这也适用于另一个方向。使用>(cmd)进行的进程替换允许您获取写入文件的命令,而是将该输出重定向到另一个命令的stdin。将某些内容插入到以输出文件名作为参数的管道中非常有用。你没有看到它那么多,因为几乎每个标准命令都会写入stdout,但我经常使用自定义的东西。这是一个人为的例子:


$ echo "hello world" | tee >(wc)
hello world
      1       2      12

#1


6  

It is called process substitution.

它被称为过程替换。

#2


6  

This is called Process Substitution

这称为过程替换

#3


1  

This is process substitution, as you have been told. I'd just like to point out that this also works in the other direction. Process substitution with >(cmd) allows you to take a command that writes to a file and instead have that output redirected to another command's stdin. It's very useful for inserting something into a pipeline that takes an output filename as an argument. You don't see it as much because pretty much every standard command will write to stdout already, but I have used it often with custom stuff. Here is a contrived example:

正如您所知,这是流程替代。我只想指出,这也适用于另一个方向。使用>(cmd)进行的进程替换允许您获取写入文件的命令,而是将该输出重定向到另一个命令的stdin。将某些内容插入到以输出文件名作为参数的管道中非常有用。你没有看到它那么多,因为几乎每个标准命令都会写入stdout,但我经常使用自定义的东西。这是一个人为的例子:


$ echo "hello world" | tee >(wc)
hello world
      1       2      12