我写了一个脚本,部分脚本如下:
#!/bin/bash
if [ "$1" == "this_script" ] then
this_script --parameters
elif [ "$1" == "other_script" ] then
other_script --parameters
else
echo "missing argument"
fi
当我运行这个脚本时,我得到了错误,
syntax error near unexpected token `elif'
`elif [ "$1" == "SWDB" ] then'
1) 行尾有问题吗?我在 Windows 上用 Notepad++ 编写了脚本,但我在编辑到 UNIX/OSX 格式下启用了 EOL 转换。
2) 如果不是行尾,错误是什么?
我在 Redhat Linux 操作系统的 bash shell 中运行这个脚本。
最佳答案
在 if [ ... ]
之后和 then
之前需要一个分号,对于 elif
也是如此:
if [ "$1" == "this_script" ]; then
# ^
# here!
# v
elif [ "$1" == "other_script" ]; then
来自 Bash manual - 3.2.4.2 Conditional Constructs :
The syntax of the if command is:
if test-commands; then consequent-commands; [elif more-test-commands; then more-consequents;] [else alternate-consequents;] fi
The
test-commands
list is executed, and if its return status is zero, theconsequent-commands
list is executed. Iftest-commands
returns a non-zero status, eachelif
list is executed in turn, and if its exit status is zero, the correspondingmore-consequents
is executed and the command completes. If ‘else alternate-consequents
’ is present, and the final command in the finalif
orelif
clause has a non-zero exit status, thenalternate-consequents
is executed. The return status is the exit status of the last command executed, or zero if no condition tested true.
关于linux - bash 中的 "syntax error near unexpected token ` elif '"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37438081/