bash 意外标记“<”附近的语法错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36144610/
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 14:23:35  来源:igfitidea点击:

syntax error near unexpected token `<'

bashshelldo-while

提问by Billy Chan

StudentAnwser=()
inputScriptFile=001.sh

while IFS= read -r line;
do
    StudentAnwser+=( "$line" )
done < <( sh $inputScriptFile test.txt )

it returns a error

它返回一个错误

foo.sh: line 22: syntax error near unexpected token `<'
foo.sh: line 22: `  done < <( sh $inputScriptFile test.txt )'

what's wrong with that? I follow the solution from other question for reading line from result

那有什么问题?我按照其他问题的解决方案从结果中读取行

回答by Lin

You get the error because process substitution(the <(some command)part) is not a standard feature (defined in POSIX) in sh, which means it may work on some OS but may not in others or in the same OS with different configuration.

您会收到错误消息,因为进程替换<(some command)部分)不是 中的标准功能(在 POSIX 中定义)sh,这意味着它可能适用于某些操作系统,但可能不适用于其他操作系统或具有不同配置的同一操作系统。

You clarified that you have #!/bin/bashat the top of your script, but I guess you still run the script via sh foo.sh, as such, #!/bin/bashwill be ignored and the script is interpreted by sh.

您澄清说您#!/bin/bash在脚本的顶部,但我猜您仍然通过 运行脚本sh foo.sh,因此,#!/bin/bash将被忽略并且脚本由sh.

I assume your default shell is bash(run echo $SHELL), so all problems are gone if you paste the script in terminal and execute.

我假设您的默认 shell 是bash(run echo $SHELL),因此如果您将脚本粘贴到终端中并执行,所有问题都会消失。

==== UPDATE ====

==== 更新 ====

Possible solution if my assumption is correct:

如果我的假设是正确的,可能的解决方案:

Leave #!/bin/bashas it is, make your script an executable by chmod +x foo.sh. Then run it directly by ./foo.sh

保持#!/bin/bash原样,使您的脚本成为可执行文件chmod +x foo.sh。然后直接运行./foo.sh