BASH脚本中出现“newline unexpected”错误

时间:2015-05-13 06:57:03

标签: bash shell

我有SHARED.SH文件:

#!/bin/sh

g_dlg_yes=1
g_dlg_no=0
g_dlg_cancel=2
g_dlg_unknown=127

show_confirm_dlg()
{
    prompt=$*
    resp=""
    while [ "$resp" != "y" ] && [ "$resp" != "n" ] && [ "$resp" != "c" ]; do
        echo "${prompt}  [y/n/c]: "
        read resp
    done
    case "$resp" in
        y ) return g_dlg_yes;;
        n ) return g_dlg_no;;
        c ) return g_dlg_cancel;;
        * ) return g_dlg_unknown;;
    Esac
}

我还有INSTALL.SH文件:

#!/bin/sh

. ./shared.sh

install_pkg()
{
    clear
    pkg_name=$*
    prompt="Do you want to install ${pkg_name}?"
    show_confirm_dlg $pkg_name
    res=$?
    if [ "$res" -eq g_dlg_cancel ]; then
        echo "Installation of $pkg_name cancelled."
        exit 2
    elif [ "$res" -eq g_dlg_no ]; then
        echo "Installation of $pkg_name rejected."
    elif [ "$res" -eq g_dlg_yes ]; then
        echo "Trying to install $pkg_name..."
        apt-get install -y $pkg_name
    else
        echo "Unknown answer. Now quitting..."
        exit 2
    fi
    echo "Press ENTER to continue..."
    read key 
}

main()
{
    install_pkg "dosbox virtualbox"
    exit $?
}

main

当我尝试运行INSTALL.SH时,会发生以下错误: ./ install.sh:22:./shared.sh:语法错误:换行意外(期待“)”)

你能帮我解决这个错误吗?

1 个答案:

答案 0 :(得分:1)

Bash命令和语句区分大小写。 您的SHARED.SH文件中的esac命令是错误的。

#!/bin/sh

g_dlg_yes=1
g_dlg_no=0
g_dlg_cancel=2
g_dlg_unknown=127

show_confirm_dlg()
{
    prompt=$*
    resp=""
    while [ "$resp" != "y" ] && [ "$resp" != "n" ] && [ "$resp" != "c" ]; do
        echo "${prompt}  [y/n/c]: "
        read resp
    done
    case "$resp" in
        y ) return g_dlg_yes;;
        n ) return g_dlg_no;;
        c ) return g_dlg_cancel;;
        * ) return g_dlg_unknown;;
    esac
}