Bash脚本:'语法错误:" fi"意想不到'在嵌套语句中

时间:2017-06-27 14:48:28

标签: bash shell syntax scripting syntax-error

我正在尝试编写一个脚本来切换HAproxy,以便在服务中断时将用户重新路由到维护页面。以下是我遇到问题的部分:

if [ "$idIsValid" = "false" ]; then
    if [ "$(cat isUp)" = "true" ]; then
            echo "Site is down! Routing to maintenance page."
            echo false > isUp
            mv haproxy.cfg haproxy.cfg.temp
            mv haproxy.cfg.other haproxy.cfg
            mv haproxy.cfg.temp haproxy.cfg.other

            #restart haproxy
            service haproxy restart
            echo "Restarting HAproxy"

    elif [ "$(cat isUp)" = "false" ]; then
            #do nothing since it has already changed
            echo "Nothing to do; service is still down."
    else
            #notify me that isUp is set to something other than true or false, or something else is wrong.
    fi
fi

当我运行时,我收到错误:

status-check.sh: Syntax error: "fi" unexpected

此错误指向第二行到最后一行的嵌套fi 。我在这里找不到语法错误。请协助!谢谢。

1 个答案:

答案 0 :(得分:1)

这是因为最后else块中没有命令。如果您删除此块或在其中放置命令(不是注释),则不会出现语法错误。