意外标记"<'附近的语法错误

Bil*_*han 1 bash shell do-while

StudentAnwser=()
inputScriptFile=001.sh

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

它返回一个错误

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

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

小智 7

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

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

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

====更新====

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

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


gan*_*opp 5

找到了另一个解决方案:进程替换不是 POSIX 兼容的功能,根据您的系统和外壳,它可能需要启用。

在循环之前脚本中使用以下行:

set +o posix
Run Code Online (Sandbox Code Playgroud)

启用它。可能是您的系统根本不支持它,那么这没有帮助。