Menu shell script help

Hi All,

I have written a shell script that show menu driven option.
My requirement is that in the menu driven option i want to select multiple choice.
i.e
if i want to select 1 or 1,2 or 1,2,3 or 2,3 etc ....
Can some one help me in that
My script.

while true; do
        echo " "
        clear
        echo "------------------------------------------------------"
        echo "    * * * * * * * * Main Menu * * * * * * * * * * *   "
        echo "------------------------------------------------------"
        echo "Please Select An Option given below."
        echo " "
        echo "[1] - TEST1"
        echo " "
        echo "[2] - TEST2"
        echo " "
        echo "[3] - TEST3"
        echo " "
        echo "[4] - ALL"
        echo " "
        echo "[q] - Quit"
        echo " "
        echo "------------------------------------------------------"
        echo -n "Enter your menu choice [1-4]:"

        read "CHOSEN_KEY"
            case $CHOSEN_KEY in

You could use set with an IFS or comma like this:

while true; do
echo " "
clear
echo "------------------------------------------------------"
echo " * * * * * * * * Main Menu * * * * * * * * * * * "
echo "------------------------------------------------------"
echo "Please Select An Option given below."
echo " "
echo "[1] - TEST1"
echo " "
echo "[2] - TEST2"
echo " "
echo "[3] - TEST3"
echo " "
echo "[4] - ALL"
echo " "
echo "[q] - Quit"
echo " "
echo "------------------------------------------------------"
echo -n "Enter your menu choice [1-4]:"

read CHOSEN_KEY

OIFS=$IFS
IFS=","
set -- $CHOSEN_KEY
IFS=$OIFS

while [ $# -ge 1 ]
do
    CHOSEN_KEY=$1
    shift
    case "$CHOSEN_KEY" in
       1) echo TEST1 ;;
       2) echo TEST2 ;;
       3) echo TEST3 ;;
       4) set 1 2 3 $@ ;;
       q) exit 0 ;;
       *) echo Invalid option $CHOSEN_KEY ;;
    esac
done
sleep 2
done

Hi Chubler_XL,

I will test that and will get back.

anyways thanks for the prompt response.

Regards,
Rajesh

---------- Post updated at 11:12 AM ---------- Previous update was at 10:47 AM ----------

Hi Chubler_XL ,

Thanks Now i am able to do multiple selections.
but now if i out if condition in in selection 1 or selection 2 i am getting below error.

./testing.sh
./testing.sh: line 35: syntax error near unexpected token `['
./testing.sh: line 35: `          if [ $CHOSEN_KEY == 1 ];'

Can you please help me out for this
Scripts:

while true; do
echo " "
clear
echo "------------------------------------------------------"
echo " * * * * * * * * Main Menu * * * * * * * * * * * "
echo "------------------------------------------------------"
echo "Please Select An Option given below."
echo " "
echo "[1] - TEST1"
echo " "
echo "[2] - TEST2"
echo " "
echo "[3] - TEST3"
echo " "
echo "[4] - ALL"
echo " "
echo "[q] - Quit"
echo " "
echo "------------------------------------------------------"
echo -n "Enter your menu choice [1-4]:"

read CHOSEN_KEY

OIFS=$IFS
IFS=","
set -- $CHOSEN_KEY
IFS=$OIFS

while [ $# -ge 1 ]
do
    CHOSEN_KEY=$1
    shift
    case "$CHOSEN_KEY" in
       1) echo TEST1 ;;
          if [ $CHOSEN_KEY == 1 ];
          then
          echo "Select Version"
          echo "[a] - Version 6"
          echo " - Version 7"
          echo "Please enter your option: "
          read "CHOSEN_VER"
          if [ $CHOSEN_VER == a ];
          then
          echo " You have chosen Version 6 installation "
          elif [ $CHOSEN_VER == b ];
          then
          echo " You have chosen Version 7 installation "
          else
          echo "Select proper category"
          fi
          fi
          break;;
       2) echo TEST2 ;;
          if [ $CHOSEN_KEY == 1 ];
          then
          echo "Select Version"
          echo "[a] - Version 6"
          echo " - Version 7"
          echo "Please enter your option: "
          read "CHOSEN_VER"
          if [ $CHOSEN_VER == a ];
          then
          echo " You have chosen Version 6 installation "
          elif [ $CHOSEN_VER == b ];
          then
          echo " You have chosen Version 7 installation "
          else
          echo "Select proper category"
          fi
          fi
          break;;
       3) echo TEST3 ;;
       4) set 1 2 3 $@ ;;
       q) exit 0 ;;
       *) echo Invalid option $CHOSEN_KEY ;;
    esac
done
sleep 2
done

 



read CHOSEN_KEY

OIFS=$IFS
IFS=","
set -- $CHOSEN_KEY
IFS=$OIFS

while [ $# -ge 1 ]
do
    CHOSEN_KEY=$1
    shift
    case "$CHOSEN_KEY" in
       1) echo TEST1 ;;
          if [ $CHOSEN_KEY == 1 ];
          then
          echo "Select Version"
          echo "[a] - Version 6"
          echo " - Version 7"
          echo "Please enter your option: "
          read "CHOSEN_VER"
          if [ $CHOSEN_VER == a ];
          then
          echo " You have chosen Version 6 installation "
          elif [ $CHOSEN_VER == b ];
          then
          echo " You have chosen Version 7 installation "
          else
          echo "Select proper category"
          fi
          fi
          break;;
       2) echo TEST2 ;;
          if [ $CHOSEN_KEY == 1 ];
          then
          echo "Select Version"
          echo "[a] - Version 6"
          echo " - Version 7"
          echo "Please enter your option: "
          read "CHOSEN_VER"
          if [ $CHOSEN_VER == a ];
          then
          echo " You have chosen Version 6 installation "
          elif [ $CHOSEN_VER == b ];
          then
          echo " You have chosen Version 7 installation "
          else
          echo "Select proper category"
          fi
          fi
          break;;
       3) echo TEST3 ;;
       4) set 1 2 3 $@ ;;
       q) exit 0 ;;
       *) echo Invalid option $CHOSEN_KEY ;;
    esac
done
sleep 2
done

Regards,
Rajesh

It's best practice to have both the string in double quotes whenever you do string comparision

if [ "$CHOSEN_VER" == "a" ];

and use numeric comparision operator for numeric as below

if [ $CHOSEN_VER -eq 1 ];

Hi ,
Still same error.

./testing.sh
./testing.sh: line 35: syntax error near unexpected token `['
./testing.sh: line 35: `          if [ $CHOSEN_KEY -eq 1 ];'
while [ $# -ge 1 ]
do
    CHOSEN_KEY=$1
    shift
    case "$CHOSEN_KEY" in
       1) echo TEST1 ;;
          if [ $CHOSEN_KEY -eq 1 ];
          echo TEST1 ;;
          then
          echo "Select Version"
          echo "[a] - Version 6"
          echo " - Version 7"
          echo "Please enter your option: "
          read "CHOSEN_VER"
          if [ "$CHOSEN_VER" == "a" ];
          then
          echo " You have chosen Version 6 installation "
          elif [ "$CHOSEN_VER" == "b" ];
          then
          echo " You have chosen Version 7 installation "
          else
          echo "Select proper category"
          fi
          fi
          break;;

---------- Post updated at 11:52 AM ---------- Previous update was at 11:39 AM ----------

Hi all ,
above problem solved but now when I select 2 options i,.e 1,2 it only echo for 1st option.
Please fin the output.

./testing.sh
------------------------------------------------------
 * * * * * * * * Main Menu * * * * * * * * * * *
------------------------------------------------------
Please Select An Option given below.

[1] - TEST1

[2] - TEST2

[3] - TEST3

[4] - ALL

[q] - Quit

------------------------------------------------------
Enter your menu choice [1-4]:1,2
You have Select 1 , now please select 1 Version
[a] - Version 6
 - Version 7
Please enter your option:
a
 You have chosen Version 6 installation

Script:-

while true; do
echo " "
clear
echo "------------------------------------------------------"
echo " * * * * * * * * Main Menu * * * * * * * * * * * "
echo "------------------------------------------------------"
echo "Please Select An Option given below."
echo " "
echo "[1] - TEST1"
echo " "
echo "[2] - TEST2"
echo " "
echo "[3] - TEST3"
echo " "
echo "[4] - ALL"
echo " "
echo "[q] - Quit"
echo " "
echo "------------------------------------------------------"
echo -n "Enter your menu choice [1-4]:"

read CHOSEN_KEY

OIFS=$IFS
IFS=","
set -- $CHOSEN_KEY
IFS=$OIFS

while [ $# -ge 1 ]
do
    CHOSEN_KEY=$1
    shift
    case "$CHOSEN_KEY" in
       1) if [ $CHOSEN_KEY -eq 1 ];
          then
          echo "You have Select $CHOSEN_KEY , now please select $CHOSEN_KEY Version"
          echo "[a] - Version 6"
          echo " - Version 7"
          echo "Please enter your option: "
          read "CHOSEN_VER"
          if [ $CHOSEN_VER == a ];
          then
          echo " You have chosen Version 6 installation "
          elif [ $CHOSEN_VER == b ];
          then
          echo " You have chosen Version 7 installation "
          else
          echo "Select proper category"
          fi
          fi
          break;;
       2) if [ $CHOSEN_KEY == 2 ];
          then
          echo "You have Select $CHOSEN_KEY , now please select $CHOSEN_KEY Version"
          echo "[a] - Version 6"
          echo " - Version 7"
          echo "Please enter your option: "
          read "CHOSEN_VER"
          if [ $CHOSEN_VER == a ];
          then
          echo " You have chosen Version 6 installation "
          elif [ $CHOSEN_VER == b ];
          then
          echo " You have chosen Version 7 installation "
          else
          echo "Select proper category"
          fi
          fi
          break;;
       3) echo TEST3 ;;
       4) set 1 2 3 $@ ;;
       q) exit 0 ;;
       *) echo Invalid option $CHOSEN_KEY ;;
    esac
done
sleep 2
done

Regards,
Rajesh

There are several problems here. Note the code marked in red above.

First the 1st ";;" on the first line of the actions for case 1 terminates the actions for that case. Having an if statement following that is not allowed. I assume you should just remove this ";;".

Second, you are in the actions to be performed when $CHOSEN_KEY expands to 1; so why do you need to test whether $CHOSEN_KEY still expands to 1 one line later?

Third, you have an echo statement after an if statement before the then keyword. And, it ends with another ";;".

I'm guessing you want something more like:

while [ $# -ge 1 ]
do
    CHOSEN_KEY="$1"
    shift
    case "$CHOSEN_KEY" in
    (1) echo TEST1
        echo 'Select Version'
        echo '[a] - Version 6'
        echo ' - Version 7'
        printf 'Please enter your option: '
        read CHOSEN_VER
        if [ "$CHOSEN_VER" = "a" ]
        then
            echo ' You have chosen Version 6 installation'
        elif [ "$CHOSEN_VER" = "b" ]
        then
            echo ' You have chosen Version 7 installation'
        else
            echo 'Select proper category'
            # I am surprised that you do not exit here instead of continuing???
        fi
        break;;

Hi All,

thanks for the reply.
when i select two option and hit enter the output show only for 1st selection and not for the second .
below check below output.

------------------------------------------------------
 * * * * * * * * Main Menu * * * * * * * * * * *
------------------------------------------------------
Please Select An Option given below.

[1] - TEST1

[2] - TEST2

[3] - TEST3

[4] - ALL

[q] - Quit

------------------------------------------------------
Enter your menu choice [1-4]:1,2
TEST1

Script:-

while true; do
echo " "
clear
echo "------------------------------------------------------"
echo " * * * * * * * * Main Menu * * * * * * * * * * * "
echo "------------------------------------------------------"
echo "Please Select An Option given below."
echo " "
echo "[1] - TEST1"
echo " "
echo "[2] - TEST2"
echo " "
echo "[3] - TEST3"
echo " "
echo "[4] - ALL"
echo " "
echo "[q] - Quit"
echo " "
echo "------------------------------------------------------"
echo -n "Enter your menu choice [1-4]:"

read CHOSEN_KEY

OIFS=$IFS
IFS=","
set -- $CHOSEN_KEY
IFS=$OIFS

while [ $# -ge 1 ]
do
    CHOSEN_KEY=$1
    shift
    case "$CHOSEN_KEY" in
       1) echo TEST1
          if [ $CHOSEN_KEY -eq 1 ];
          then
          echo "You have Select $CHOSEN_KEY , now please select $CHOSEN_KEY Version"
          echo "[a] - Version 6"
          echo " - Version 7"
          echo "Please enter your option: "
          read "CHOSEN_VER"
          if [ $CHOSEN_VER == a ];
          then
          echo " You have chosen Version 6 installation "
          elif [ $CHOSEN_VER == b ];
          then
          echo " You have chosen Version 7 installation "
          else
          echo "Select proper category"
          fi
          fi
          exit ;
          break;;

       2) echo TEST2
          if [ $CHOSEN_KEY == 2 ];
          then
          echo "You have Select $CHOSEN_KEY , now please select $CHOSEN_KEY Version"
          echo "[a] - Version 6"
          echo " - Version 7"
          echo "Please enter your option: "
          read "CHOSEN_VER"
          if [ $CHOSEN_VER == a ];
          then
          echo " You have chosen Version 6 installation "
          elif [ $CHOSEN_VER == b ];
          then
          echo " You have chosen Version 7 installation "
          else
          echo "Select proper category"
          fi
          fi
          exit ;
          break;;

       3) echo TEST3 ;;
       4) set 1 2 3 $@ ;;
       q) exit 0 ;;
       *) echo Invalid option $CHOSEN_KEY ;;
    esac
done
sleep 2
done

I see that you chose to ignore most of the suggestions I made last time and added exits outside your nested if statements statement (instead of as part of the else clauses that identify invalid input). Those exits will terminate your script whenever you select 1 or 2 in your main menu.

Please explain why you think it is necessary to test the value of $CHOSEN_KEY twice in cases 1 and 2:

case "$CHOSEN_KEY" in
(1) if [ "$CHOSEN_KEY" -eq 1 ]
    then process case 1
    fi;;
(2) if [ "$CHOSEN_KEY" -eq 2 ]
    then process case 2
    fi;;
esac

is an inefficient and confusing way to specify the same actions as:

case "$CHOSEN_KEY" in
(1) process case 1;;
(2) process case 2;;
esac

Hi Nawrajesh,

Try to remove the below lines from your code

 
exit ;
break;;

and replace it with

echo "End of Seclection" ;;

It worked fine for me while testing.