Shell脚本错误:语法错误:“(”意外(预期为“ fi”)

时间:2019-06-19 10:17:47

标签: shell

我在以下shell脚本中遇到语法错误,但我不知道为什么:

./query_certs.sh: 22: ./query_certs.sh: Syntax error: "(" unexpected (expecting "fi")

是因为嵌套的if-else语句吗?

谢谢。

# Check how many arguments we received
if [ "$#" -gt 2 ]; then
    echo "Usage: $0 [MODE] DESTINATION_IP" >&2
    echo "MODE may be one of the following" >&2
    echo "none - shell script will ask interactively" >&2
    echo "HTTP - query the certificate from HTTPS (port 443)" >&2
    echo "LDAP - query the certificate from an AD  (port 636" >&2
    echo "FTP - query the certificate from an FTPs (ports 990/989)" >&2
    exit 1
elif [ "$#" == 2 ]; then
    MODE=$1
    if [ "$MODE" == "HTTP" ]; then
        QUERY="foo"
    elif [ "$MODE" == "LDAP" ]; then
        QUERY="bar"
    elif [ "$MODE" == "FTP" ]; then
        QUERY="baz"
    else
        echo "Please choose a mode  (HTTP | LDAP | FTP):" # <<< that's line 22
        options=("HTTP" "LDAP" "FTP";)
        select opt  in "${options[@]}" do
            case $opt in
                "HTTP")
                    echo "Selected HTTP"
                    ;;
                "LDAP")
                    echo "Selected LDAP"
                    ;;
                "FTP")
                    echo "Selected FTP"
                    ;;
            esac
        done
    fi
fi

1 个答案:

答案 0 :(得分:0)

好的,我自己修复了。我在原始帖子中没有提到的一件重要事情(现在添加了它,因此可能会对其他人有所帮助):最初我有以下提示:

#!/usr/bin/env sh

因为我想使POSIX兼容。似乎我正在使用的某些例程不符合POSIX。因此,我改为使用bash。此外,还有更多的错误,其修复如下:

# Check how many arguments we received
if [ "$#" -gt 2 ]; then
    echo "Usage: $0 [MODE] DESTINATION_IP" >&2
    echo "MODE may be one of the following" >&2
    echo "none - shell script will ask interactively" >&2
    echo "HTTP - query the certificate from HTTPS (port 443)" >&2
    echo "LDAP - query the certificate from an AD  (port 636" >&2
    echo "FTP - query the certificate from an FTPs (ports 990/989)" >&2
    exit 1
elif [ "$#" = 2 ]; then
    MODE=$1
    if [ "$MODE" = "HTTP" ]; then
        QUERY="foo"
    elif [ "$MODE" = "LDAP" ]; then
        QUERY="bar"
    elif [ "$MODE" = "FTP" ]; then
        QUERY="baz"
    else
        PS3='Please choose a mode  (HTTP | LDAP | FTP):'
        options=("HTTP" "LDAP" "FTP")
        select opt  in "${options[@]}" do
            case $opt in
                "HTTP")
                    echo "Selected HTTP"
                    ;;
                "LDAP")
                    echo "Selected LDAP"
                    ;;
                "FTP")
                    echo "Selected FTP"
                    ;;
            esac
        done
    fi
fi