Presenting the user a menu in Bash ...

Hello,

I am trying to write a bash script function to present the user a menu of options. The following is what I have so far:

function MainMenu {
    while [[ "$USR_CHOICE" !== "4" || "$USR_CHOICE" !== "four" || "$USR_CHOICE" !== "Four" ]]
    do
        echo "--------------------------------------------------------------------------------"
        echo "-----------------------------------Main Menu------------------------------------"
        echo "--------------------------------------------------------------------------------"
        echo "Select from the following options:"
        echo "----------------------------------"
        echo ""
        echo "        1. Option One"
        echo "        2. Option Two"
        echo "        3. Option Three"
        echo "        4. Exit"
        echo ""
        read -r -p "Type in the desired option: " USR_CHOICE
        case $USR_CHOICE in
        1|"1"|one|One)
            ;;
        2|"2"|two|Two)
            ;;
        3|"3"|three|Three)
            ;;
        4|"4"|four|Four)
            ;;
        *)
            #Error occured in script
            ;;
        esac
    done
}

The problem is that when the script is run I get the following error message back:

./deploy.sh: line 14: conditional binary operator expected
./deploy.sh: line 14: syntax error near `!=='
./deploy.sh: line 14: ` while [[ "$USR_CHOICE" !== "4" || "$USR_CHOICE" !== "four" || "$USR_CHOICE" !== "Four" ]]'

So, the issue I am having is how can I run the loop of the manu menu as my function is intended to provide?

Thanks for the time.

Change !== vs != and try again.

That worked!

I feel kinda dumb. Oh well. Thanks for the assistance!

Chris K.

No need to feel dumb - happens to everyone sometimes :slight_smile:

I recommend using standard syntax so that your script is portable (and fixing the !== operator):

MainMenu() {
  while [ "$USR_CHOICE" != "4" ] || [ "$USR_CHOICE" != "four" ] || [ "$USR_CHOICE" != "Four" ]

However, are you sure of the logic?

I would use while : and put the exit in the case statement.

You are testing for the same thing twice. You don't need both 1 and "1".

4|[fF]our)
           break ;;