BASH 脚本中的“换行意外”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30207723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 12:59:24  来源:igfitidea点击:

'newline unexpected' Error in BASH script

bashshell

提问by Danijel Janosevic

I have SHARED.SH file:

我有 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
}

Also I have INSTALL.SH file:

我还有 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

When I try to run INSTALL.SH the following error occurs: ./install.sh: 22: ./shared.sh: Syntax error: newline unexpected (expecting ")")

当我尝试运行 INSTALL.SH 时出现以下错误 :./install.sh: 22: ./shared.sh: Syntax error: newline unexpected (expecting ")")

Could you help me with this error, please?

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

采纳答案by Gowtham Ganesh

Bash commands and statements are case-sensitive. The esaccommand in your SHARED.SH file is in the wrong case.

Bash 命令和语句区分大小写。esacSHARED.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
}