Danijel Janosevic
Danijel Janosevic

Reputation: 33

'newline unexpected' Error in BASH script

I have SHARED.SH file:

#!/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:

#!/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 ")")

Could you help me with this error, please?

Upvotes: 3

Views: 27226

Answers (1)

Gowtham Ganesh
Gowtham Ganesh

Reputation: 340

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

#!/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
}

Upvotes: 1

Related Questions