Unix elif语法错误

时间:2014-06-17 20:06:00

标签: unix

获取错误:

syntax error near unexpected token `elif' 
`elif [ "$PROJECT_DIR" = "Automation" ] then'

我在Textpad ++中保存了Unix行格式,所以我不确定问题是什么......

#!/bin/bash

....

if [ "$PROJECT_DIR" = "Utility" ] then

    echo "$PROJECT_DIR: Compiling..."
    javac -sourcepath $SOURCE -classpath $CLASSPATH -d $OUTPUT $SOURCE/*.java -deprecation

    echo "$PROJECT_DIR: Compiling..."
    javac -sourcepath $SOURCE -classpath $CLASSPATH -d $OUTPUT $SOURCE/*.java -deprecation

elif [ "$PROJECT_DIR" = "Automation" ] then

    echo "$PROJECT_NAME: Compiling..."
    javac -sourcepath $SOURCE -classpath $CLASSPATH -d $OUTPUT $SOURCE/*.java

elif [ "$PROJECT_DIR" = "Sync" ] then

    echo "$PROJECT_DIR: Compiling..."
    javac -sourcepath $SOURCE -classpath $CLASSPATH -d $OUTPUT $SOURCE/*.java -deprecation 
    echo "$PROJECT_DIR: Compiling..."
    javac -sourcepath $SOURCE -classpath $CLASSPATH -d $OUTPUT $SOURCE/*.java -deprecation 
    echo "$PROJECT_DIR: Compiling..."
    javac -sourcepath $SOURCE -classpath $CLASSPATH -d $OUTPUT $SOURCE/*.java -deprecation 

fi

2 个答案:

答案 0 :(得分:1)

问题是您需要在]then之间使用分号(或换行符)。

[不是shell语法的一部分。它是一个内置命令,相当于test命令(除了[需要]作为其最后一个参数,而test不是{0}}

(另一方面,关键字iftheneliffi以及分号部分shell的语法。)

所以如果你写:

if [ "$PROJECT_DIR" = "Utility" ] then

单词then只是[命令的另一个参数。

将每次] then更改为] ; then都可以解决问题。

答案 1 :(得分:0)

尝试这样做(删除大部分代码行以保持答案简短)。不需要上次elif条件elif [ "$PROJECT_DIR" = "Sync" ]。它应该只是else部分,如下所示。

if [ "$PROJECT_DIR" = "Utility" ] 
then
    echo "$PROJECT_DIR: Compiling..."    
elif [ "$PROJECT_DIR" = "Automation" ] 
then
    echo "$PROJECT_NAME: Compiling..."    
else 
    echo "$PROJECT_DIR: Compiling..."
fi