语法错误:“}”意外(期待“fi”)

时间:2013-10-23 16:36:08

标签: bash

这是一个从Atlassian下载的简单shell脚本,稍作调整,以满足目前为止找到的所有建议:

#!/bin/sh

# RUN_AS: The user to run fisheye as. Its recommended that you create a separate user account for security reasons
RUN_AS=fisheye

# FISHEYE_HOME: The path to the FishEye installation. Its recommended to create a symbolic link to the latest version so
# the process will still work after upgrades.
FISHEYE_HOME="/opt/atlassian/fisheye"

fisheyectl() {
        ARGS="--Xdisable-tags --Xenable-git-content-hash-resolving-heuristic"
        CMD="$FISHEYE_HOME/bin/fisheyectl.sh $1"
        if [ $1 == "run" ]
        then
            CMD="nohup $CMD $ARGS >> $FISHEYE_HOME/var/log/nohup.out &";
        fi
        if [ "$USER" != "$RUN_AS" ]
        then
                su - "$RUN_AS" -c "$CMD";
        else
                sh -c "$CMD";
        fi
}

case "$1" in
        start)
                fisheyectl run
                ;;
        stop)
                fisheyectl stop
                ;;
        restart)
                fisheyectl stop
                sleep 10
                fisheyectl run
                ;;
        *)
                echo "Usage: $0 {start|stop|restart}"
esac

exit 0

我已经通过dos2unix本地运行了这个。执行时:

fisheye: 23: fisheye: Syntax error: "}" unexpected (expecting "fi")

有什么建议吗?

3 个答案:

答案 0 :(得分:2)

我遇到了类似的问题,事实证明我必须将#/ bin / sh行改为#/ bin / bash

答案 1 :(得分:0)

更改

if [ $1 == "run" ]

if [ ${1:-X} = "run" ]

答案 2 :(得分:0)

改变这个:

if [ $1 == "run" ]

到此:

if [ "$1" = "run" ]

在bash的内置[命令中,===的同义词,但并非[test)命令的所有实现都能识别===是字符串相等性比较的原始运算符 - 您的脚本具有#!/bin/sh,可能与bash兼容也可能不兼容。

我还在$1附近添加了双引号。在这种情况下,它们并不是绝对必要的,因为你总是用单个单词参数调用fisheyectl(),但通常将变量引用括在单引号中是个好主意。 (如果没有引号,如果您在没有参数的情况下调用fisheyectl(),则会出现if [ = "run" ],这是语法错误。)