gpt4 book ai didi

bash - 令人费解的 Bash 语法——重定向到循环

转载 作者:行者123 更新时间:2023-11-29 09:09:30 25 4
gpt4 key购买 nike

通过 shellcheck.net 运行脚本 我被告知 " for loops over find output are fragile."很公平,带空格的文件名总是很棘手。

建议的替换代码(其中之一)是这样的:

while IFS= read -r -d '' file
do
#do stuff
done < <(find . -name '*' -print0)

我熟悉 <运算符用于将文件中的文本输入到命令中,并且很明显 find 的结果被送入循环。因此,当我第一次看到它时,我认为它是某些格式问题的受害者,并尝试“清理”最后一行。但是删除 < < 之间的空格导致语法错误,在 <( 中添加一个也是如此.

那么 < <(cmd) 是什么?构建做什么?抱歉,如果这是一个简单或多余的问题,搜索这些东西并不容易!

最佳答案

<input redirection正如你所说。

<(Process Substitution .

Process substitution is supported on systems that support named pipes (FIFOs) or the /dev/fd method of naming open files. It takes the form of

<(list)

or

>(list)

The process list is run with its input or output connected to a FIFO or some file in /dev/fd. The name of this file is passed as an argument to the current command as the result of the expansion. If the >(list) form is used, writing to the file will provide input for list. If the <(list) form is used, the file passed as an argument should be read to obtain the output of list. Note that no space may appear between the < or > and the left parenthesis, otherwise the construct would be interpreted as a redirection.

When available, process substitution is performed simultaneously with parameter and variable expansion, command substitution, and arithmetic expansion.

你不能将两者结合起来<标记,因为那样就变成了 here document token 。

您不能在 < 之间添加空格和 (因为那样它就不再是进程替换 token 。

关于bash - 令人费解的 Bash 语法——重定向到循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33812675/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com