Node.js Bash语法问题&x27;语法错误:意外的";“做”;(预期为“fi”)&x27;
我有一个sh脚本,我在Windows和Mac/Linux机器上使用,并且似乎正常工作没有问题Node.js Bash语法问题&x27;语法错误:意外的";“做”;(预期为“fi”)&x27;,node.js,bash,shell,docker,sh,Node.js,Bash,Shell,Docker,Sh,我有一个sh脚本,我在Windows和Mac/Linux机器上使用,并且似乎正常工作没有问题 #!/bin/bash if [ -z "$jmxname" ] then cd ./tests/Performance/JMX/ || exit echo "-- JMX LIST --" # set the prompt used by select, replacing "#?" PS3="Use number to select a file or 'stop' to cancel: "
#!/bin/bash
if [ -z "$jmxname" ]
then
cd ./tests/Performance/JMX/ || exit
echo "-- JMX LIST --"
# set the prompt used by select, replacing "#?"
PS3="Use number to select a file or 'stop' to cancel: "
# allow the user to choose a file
select jmxname in *.jmx
do
# leave the loop if the user says 'stop'
if [[ "$REPLY" == stop ]]; then break; fi
# complain if no file was selected, and loop to ask again
if [[ "$jmxname" == "" ]]
then
echo "'$REPLY' is not a valid number"
continue
fi
# now we can use the selected file, trying to get it to run the shell script
rm -rf ../../Performance/results/* && cd ../jmeter/bin/ && java -jar ApacheJMeter.jar -Jjmeter.save.saveservice.output_format=csv -n -t ../../JMX/"$jmxname" -l ../../results/"$jmxname"-reslut.jtl -e -o ../../results/HTML
# it'll ask for another unless we leave the loop
break
done
else
cd ./tests/Performance/JMX/ && rm -rf ../../Performance/results/* && cd ../jmeter/bin/ && java -jar ApacheJMeter.jar -Jjmeter.save.saveservice.output_format=csv -n -t ../../JMX/"$jmxname" -l ../../results/"$jmxname"-reslut.jtl -e -o ../../results/HTML
fi
我现在正在尝试使用Docker容器做一些事情,并使用了一个节点:alpine image,因为我的项目的其余部分是基于NodeJS的,但是由于某些原因,脚本将不会在Docker容器中运行,并给出以下内容-
line 12: syntax error: unexpected "do" (expecting "fi")
我怎样才能解决这个问题?该脚本似乎适用于迄今为止运行它的每个系统,并且没有出现任何问题。错误消息表明该脚本以“/bin/sh”而不是/bin/bash”的形式执行。您可以看到带有“/bin/sh-nscript.sh”的消息 检查如何调用脚本。在不同的系统上/bin/sh符号链接到bash或其他功能不太丰富的shell 特别是,问题在于bash中包含的
select
语句,但它不是POSIX标准的一部分
另一个选项是docker上的bash默认设置为与POSIX兼容@dash-o是正确的,并且添加-
RUN apk update && apk add bash
我的dockerfile将bash添加到容器中,现在它工作正常:)如何启动脚本?通过npm命令“sh node_modules/ukri test framework/scripts/runPerformance.sh”do和done对
有意义,而或for
在shell脚本中循环我认为这与POSIX sh有关,因为它是bash syntaxit,所以它在我使用过的所有windows/mac/linux机器上都能工作似乎很奇怪,但现在docker容器中出现了问题……啊,这可能是有道理的,我无法仅使用/bin/bash连接到容器/bin/sh将对此进行更深入的研究,因为这将得到我的答案,我认为…感谢您提供了更详细的信息:POSIX允许,但不要求,select
被视为保留字。在这里,shell将其解析为另一个命令名。因此,对于解析器来说,看到do
而没有看到for
、while
或直到
为止是一件令人惊讶的事情。