简体   繁体   English

Bash命令使用重定向运算符的“读取”行为

[英]Bash command “read” behaviour using redirection operator

If I execute the following command: 如果我执行以下命令:

> read someVariable _ < <(echo "54 41")

and: 和:

> echo $someVariable

The result is: 54 . 其结果是: 54

What does < < (with spaces) do? < < (带空格)做什么?

Why is _ giving the first word from the result in the "echo" command? 为什么_在“ echo”命令中从结果中给出第一个单词?

The commands above are just example. 上面的命令仅是示例。

Thanks a lot 非常感谢

Process Substitution 流程替代

As tldp.org explains, 正如tldp.org解释的那样,

Process substitution feeds the output of a process (or processes) into the stdin of another process. 进程替换将一个或多个进程的输出馈送到另一个进程的标准输入中。

So in effect this is similar to piping stdout of one command to the other , eg echo foobar barfoo | wc 因此,实际上,这类似于将一个命令的stdout传递给另一个命令,例如echo foobar barfoo | wc echo foobar barfoo | wc . echo foobar barfoo | wc But notice: in the [bash manpage][3] you will see that it is denoted as <(list) . 但是请注意:在[bash手册页] [3]中,您会看到它表示为<(list) So basically you can redirect output of multiple (!) commands. 因此,基本上,您可以重定向多个(!)命令的输出。

Note: technically when you say < < you aren't referring to one thing, but two redirection with single < and process redirection of output from <( . . .). 注意:从技术上讲,当您说<<时,您并不是在指一件事,而是指两个具有单个<重定向和处理来自<(。。。)的输出重定向。

Now what happens if we do just process substitution? 现在,如果仅进行过程替换会发生什么?

$ echo <(echo bar)
/dev/fd/63

As you can see, the shell creates temporary file descriptor /dev/fd/63 where the output goes. 如您所见,shell在输出创建的地方创建了临时文件描述符/dev/fd/63 That means < redirects that file descriptor as input into a command. 这意味着<将文件描述符重定向为命令的输入。

So very simple example would be to make process substitution of output from two echo commands into wc: 因此,非常简单的示例是将两个回显命令的输出替换为wc:

$ wc < <(echo bar;echo foo)
      2       2       8

So here we make shell create a file descriptor for all the output that happens in the parenthesis and redirect that as input to wc .As expected, wc receives that stream from two echo commands, which by itself would output two lines, each having a word, and appropriately we have 2 words, 2 lines, and 6 characters plus two newlines counted. 因此,在这里我们让shell为括号中出现的所有输出创建一个文件描述符,并将其重定向为wc的输入。正如预期的那样,wc从两个echo命令接收该流,该命令本身将输出两行,每行有一个单词,并且适当地,我们有2个单词,2行和6个字符加上两个换行符。

Side Note : Process substitution may be referred to as a bashism (a command or structure usable in advanced shells like bash, but not specified by POSIX), but it was implemented in ksh before bash's existence as ksh man page . 侧注 :进程替换可以被称为bashism(指令或结构在先进壳诸如bash可用的,但不是由POSIX指定),但它是在ksh的bash的存在作为前实现ksh的手册页 Shells like tcsh and mksh however do not have process substitution. 但是,像tcshmksh这样的shell没有进程替代。 So how could we go around redirecting output of multiple commands into another command without process substitution? 那么,如何在不进行进程替换的情况下将多个命令的输出重定向到另一个命令呢? Grouping plus piping! 分组加管道!

$ (echo foo;echo bar) | wc
      2       2       8

Effectively this is the same as above example, However, this is different under the hood from process substitution, since we make stdout of the whole subshell and stdin of wc [linked with the pipe][5]. 实际上,这与上面的示例相同,但是,这与进程替换在本质上是不同的,因为我们将整个子外壳的stdout和wc stdin [与管道链接] [5]关联在一起。 On the other hand, process substitution makes a command read a temporary file descriptor. 另一方面,进程替换使命令读取临时文件描述符。

So if we can do grouping with piping, why do we need process substitution? 因此,如果我们可以使用管道进行分组,为什么我们需要流程替代? Because sometimes we cannot use piping. 因为有时我们不能使用管道。 Consider the example below - comparing outputs of two commands with diff (which needs two files, and in this case we are giving it two file descriptors) 考虑下面的示例-比较两个命令与diff输出(需要两个文件,在这种情况下,我们给它两个文件描述符)

diff <(ls /bin) <(ls /usr/bin)

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

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