Menu shell script help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Menu shell script help
# 1  
Old 11-22-2013
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.
Code:
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


Last edited by Scott; 11-22-2013 at 01:50 AM.. Reason: Please use code tags
This User Gave Thanks to Nawrajesh For This Post:
# 2  
Old 11-22-2013
You could use set with an IFS or comma like this:

Code:
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

# 3  
Old 11-22-2013
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.

Code:
./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:

Code:
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 "[b] - 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 "[b] - 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 "[b] - 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 "[b] - 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

Last edited by Scott; 11-22-2013 at 01:50 AM.. Reason: Code tags
# 4  
Old 11-22-2013
It's best practice to have both the string in double quotes whenever you do string comparision
Code:
if [ "$CHOSEN_VER" == "a" ];

and use numeric comparision operator for numeric as below
Code:
if [ $CHOSEN_VER -eq 1 ];

# 5  
Old 11-22-2013
Hi ,
Still same error.

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

Code:
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 "[b] - 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.

Code:
./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
[b] - Version 7
Please enter your option:
a
 You have chosen Version 6 installation

Script:-
Code:
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 "[b] - 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 "[b] - 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

Moderator's Comments:
Mod Comment I would recommend that you read the private messages you have received regarding the use of code tags...

Last edited by Scott; 11-22-2013 at 02:15 AM.. Reason: Code tags, please...
# 6  
Old 11-22-2013
Quote:
Originally Posted by Nawrajesh
Hi ,
Still same error.

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

Code:
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 "[b] - 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;;

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:
Code:
while [ $# -ge 1 ]
do
    CHOSEN_KEY="$1"
    shift
    case "$CHOSEN_KEY" in
    (1) echo TEST1
        echo 'Select Version'
        echo '[a] - Version 6'
        echo '[b] - 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;;

# 7  
Old 11-22-2013
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.
Code:
------------------------------------------------------
 * * * * * * * * 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:-
Code:
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 "[b] - 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 "[b] - 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


Last edited by Scott; 11-22-2013 at 04:23 AM.. Reason: Use code tags, PLEASE...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to include menu based options in Shell script?

Hi Friends, I have a menu based tool which requires input/option to proceed further. How to make a shell script ? eg: menu looks like Get_data.sh to continue (y/n) : Here I need to key in "y" to proceed. I want to prepare a script which should consider option y. (5 Replies)
Discussion started by: suresh3566
5 Replies

2. Shell Programming and Scripting

Automate the menu options using shell script

I have a menu option which will look as follows Select a menu option 1.change password 2.login as root user 3.show system version 4.quit Select> 1 please enter the new password: unix reenter the new password: unix press any key to enter (then displays again the menu options to enter the... (4 Replies)
Discussion started by: shivakumar6g
4 Replies

3. Shell Programming and Scripting

A selection menu in a shell script

I'm writing a shell script and have a problem with selection when I issue the command, is there a way to automatically choose a selection number one after a selection menue appear Command 1-choice 2- choice 3-choice Thanks Sara (3 Replies)
Discussion started by: Sara_84
3 Replies

4. Shell Programming and Scripting

Shell script menu problem

I have tried searching the forum but i haven't found a solution for this. I have a shell script that presents the users with menus. The menus branch out to sub menus. It is all hunky dory as long as i traverse forward. But if i am in a sub menu and return to the previous menu and choose any... (11 Replies)
Discussion started by: goddevil
11 Replies

5. Shell Programming and Scripting

Menu using shell script

Hi, I need to have a shell script for the below need. 1. Menu with one heading and 4 options. 2. the heading and 4 options are taken from a file. File entry ====== Heading1|option1|option2|option3|option4| Heading2|option1|option2|option3|option4| 3. the user entries must be captured in... (9 Replies)
Discussion started by: umastinu
9 Replies

6. Shell Programming and Scripting

Shell script menu

hi guys, how would you do the following? I have a menu with 5 options in my shell script: 1. Run function 1 against files 2. Run function 2 against files 3. Run function 3 against files 4. Run function 4 against files 5. Run function 5 against files I'd like to be able to run multiple... (10 Replies)
Discussion started by: rich@ardz
10 Replies

7. Homework & Coursework Questions

Menu Driven Shell Script which accepts1 to 5 options

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: 1) Write a Menu Driven Shell Script which accepts1 to 5 options and performs the following actions for... (1 Reply)
Discussion started by: vaghya
1 Replies

8. Shell Programming and Scripting

shell script to alter grub menu.lst

Hi folks, I have a dual-boot Ubuntu/Windows machine and I wanted to create a script to change the menu.lst file so it will change the default boot partition (this is so I can reload the machine remotely and allow it to boot to the Windows partition). Today I have to sudo cp a template file I... (1 Reply)
Discussion started by: ppucci
1 Replies

9. Shell Programming and Scripting

Unix Shell Script: With Menu Option

I am attempting to create a shell script with the following capaciblities: 1. Listed options to choice from 2. Use to perform awk statements 3. Print a report with the awk results My questions are 1. How do I select more than one file for option #5 and #6 2. How to I create an... (11 Replies)
Discussion started by: jroberson
11 Replies

10. UNIX for Dummies Questions & Answers

Changing korn shell script text Menu colors?

Is it possible to change the color of text in a korn shell script Menu? I can change the color of session text through my telnet client but I want to be able to change color text in the Korn shell menu to highlight certain items. (6 Replies)
Discussion started by: darthur
6 Replies
Login or Register to Ask a Question