繁体   English   中英

shell 中的“< <(command args)”是什么意思?

[英]What does "< <(command args)" mean in the shell?

当递归循环遍历包含空格的文件的文件夹时,我使用的 shell 脚本是这种形式,从inte.net复制:

    while IFS= read -r -d $'\0' file; do
      dosomethingwith "$file"        # do something with each file
    done < <(find /bar -name *foo* -print0)

我想我了解 IFS 位,但我不明白 ' < <(...) ' 字符的含义。 显然这里有某种管道。

很难用 Google 搜索“< <”或“<(”,你看。我试过“尖括号”和“小于括号”,但没有找到任何东西。

<()在手册中称为进程替换 ,类似于管道,但传递的形式为/dev/fd/63而不是使用stdin。

<从命令行上命名的文件中读取输入。

这两个操作符一起完全像管道一样运行,因此可以重写为

find /bar -name *foo* -print0 | while read line; do
  ...
done

<重定向到stdin。

<()似乎是某种反向管道,如页面所述:

find /bar -name *foo* -print0 | \
while IFS= read -r -d $'\0' file; do
  dosomethingwith "$file"        # do something with each file
done

将无法工作,因为while循环将在子shell中执行,并且您将丢失循环中所做的更改

<(命令)是进程替换。 基本上,它创建一个称为“命名管道”的特殊类型的文件,然后将命令的输出重定向为命名管道。 例如,假设您想要翻阅超大目录中的文件列表。 你可以这样做:

ls /usr/bin | more

或这个:

more <( ls /usr/bin )

但不是这个:

more $( ls /usr/bin )

当您进一步调查时,原因就变得清晰了:

~$ echo $( ls /tmp )
gedit.maxtothemax.436748151 keyring-e0fuHW mintUpdate orbit-gdm orbit-maxtothemax plugtmp pulse-DE9F3Ei96ibD pulse-PKdhtXMmr18n ssh-wKHyBU1713 virtual-maxtothemax.yeF3Jo
~$ echo <( ls /tmp )
/dev/fd/63
~$ cat <( ls /tmp )
gedit.maxtothemax.436748151
keyring-e0fuHW
mintUpdate
orbit-gdm
orbit-maxtothemax
plugtmp
pulse-DE9F3Ei96ibD
pulse-PKdhtXMmr18n
ssh-wKHyBU1713
virtual-maxtothemax.yeF3Jo

/ dev / fd /无论是什么行为都像是一个文本文件,在括号之间输出命令。

大多数情况下,当您需要将文件传递给命令但使用另一个命令的 output 而不是文件,从而避免了提前创建和填充该文件的需要。

对于前。 假设你想比较 2 个命令的 output,你可以这样做:

diff <(command1) <(command2)

<<运算符引入了一个here-document ,它将另一个命令的输出作为第一个命令的输入。

更新

好的,所以自从我15年前上次使用它以来,它们必须为shell添加一些东西。
请不要理会。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM