3

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?

2
  • 1
    You have the esac statement in wrong case. Commented May 13, 2015 at 6:59
  • 3
    I would recommend feeding the code into shellcheck.net as a first approach also - you have several other errors in the script which it points out Commented May 13, 2015 at 7:02

1 Answer 1

1

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
}
Sign up to request clarification or add additional context in comments.

1 Comment

In addition, the return statements need to have a $ before the variable references because they won't be valid return values otherwise - you can only return a number, and without a $, they're strings when used like that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.