Bash 语法映射文件输入重定向

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29665142/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 12:48:34  来源:igfitidea点击:

Bash syntax mapfile input redirection

bashredirectinput

提问by Sujay Phadke

Could someone explain me this syntax for mapfile input redirection?

有人可以向我解释这种映射文件输入重定向的语法吗?

mapfile -t array < <(./inner.sh)

(from this StackOverflow answerby gniourf_gniourf)

(来自gniourf_gniourf 的StackOverflow 回答

As I understand, the first "<" is to take input from the output of whatever is on the right side of it. But what is the <( ... ) syntax? Why is the second "<" required?

据我了解,第一个“<”是从其右侧的任何输出中获取输入。但是 <( ... ) 语法是什么?为什么需要第二个“<”?

回答by Etan Reisner

The reason the linked examplelooks like this:

链接示例的原因如下所示:

wc <(cat /usr/share/dict/linux.words)

And the mapfile answerlooks like this:

映射文件的回答是这样的:

mapfile -t array < <(./inner.sh)

Is because of the difference in the wcand mapfilecommands and how the substituted process needs to be given to command.

是因为wcmapfile命令的不同以及需要如何将替换过程赋予命令。

As anishsane says the expansion of <(command)is a filename and can be used anywhere a filename can be used.

正如 anishsane 所说,扩展<(command)是一个文件名,可以在任何可以使用文件名的地方使用。

wctakes filenames as arguments so it can be used directly as an argument.

wc将文件名作为参数,因此可以直接用作参数。

mapfilereads from standard input so to get it to read from a specific file you use you redirect standard input from the file </path/to/somefilebut as I just said the expansion of <(command)is a filename so you can use that in the input redirection.

mapfile从标准输入读取以便让它从您使用的特定文件中读取您从文件重定向标准输入,</path/to/somefile但正如我刚才所说的扩展<(command)是一个文件名,因此您可以在输入重定向中使用它。

However, you cannot just concat the two bits directly (the way you can with a file name/path) because <<is also a valid shell construct (a here document) and that would be ambiguous to the shell. So to avoid that you need to space out the two <characters and end up with < <(command)(which is analogous to < filewhich is perfectly legal).

但是,您不能直接连接这两个位(您可以使用文件名/路径的方式),因为<<这也是一个有效的 shell 构造(此处的文档),这对 shell 来说是不明确的。因此,为了避免您需要将两个<字符隔开并以< <(command)(这类似于< file完全合法的)结束。