Trouble in getting user input while using CASE statement in UNIX


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Trouble in getting user input while using CASE statement in UNIX
# 1  
Old 02-02-2012
Trouble in getting user input while using CASE statement in UNIX

i want to get user input like this

please tell which option to chose
1. mango 2. tango 3. rango

if user chooses mango

then it should execute a set of statements and again ask like this

what do you want to do
1.add 2.subtract 3.exit

when i choose exit it should goto my previous menu asking again (please tell which option to chose
1. mango 2. tango 3. rango )

i have did coding to an extent like this
Code:
#!/usr/bin/ksh
#

echo -e -n "choose names as listed in the below format 1. mango 2. tango 3. rango 4.exit"
read answer
echo "$answer"

while true
do
   case $answer in
   mango) DT=$(date)
   echo "******************* $DT ***********************" 
   echo -e "Specify what options 1.add 2.subtract 3.exit"
   read input
   #
   if [ "add" = "$input" ]
   then
         echo "add"
   elif [ "exit" = "$input" ]
   then
        exit 0;;          
                        #iam getting error at the place exit 0;;
   elif [ "subtract" = "$input" ]
           echo "sub"
   else
           echo "!! must specify add or subtract"
   fi
 
   #
   tango) echo "tango"
   break;;
   #
   rango) echo "rango"
   break;;
   exit) exit 0;
   break;;
   #
   *) echo "please enter correct value" read ;;
   break;;
   # --- VBE --- adding missing done...--- #
done

As you can see first it will ask me to choose from 6 options
1. mango 2. tango 3. rango 4.exit

. If for example if i chose 1. mango
then it will ask
1.add 2.subtract 3.exit
now if i chose exit . it should go back to main menu (THIS IS WHAT IAM NOT ABLE TO DO) and ask
1. mango 2. tango 3. rango 4.exit
if i give exit here then only script should exit.

Please help how to do this one in the existing scrit. if not possible then modify and do in whicvere way u wish to do to to get the final result. I have just put an skeleton overview of the script. If anyone have any better approach on this then please advice as to how to arrive to this solution.

Moderator's Comments:
Mod Comment Please use next time code tags for your code and data

Last edited by vbe; 02-02-2012 at 11:07 AM.. Reason: indent code for reading...
# 2  
Old 02-02-2012
Quote:
exit 0;;
#iam getting error at the place exit 0;;
The ;; are in the wrong place, they should be after the fi which terminates your if statement. In a case statement the ;; terminates each separate condition.



Quote:
*) echo "please enter correct value" read ;;
break;;
The last two lines are a bit iffy. Probably best to just output an error message because the script is going to ask the question again on the next iteration of the "while true" loop (if you move the question to within the loop). Also, lose the break;; line because it is totally out of context.
Add the "esac" to complement the original "case".
Add the "done" to complement the original "do".
Code:
*) echo "please enter correct value" ;;
esac
done

The initial question to populate $answer should probably be inside the do-done loop or the users will neve get the chance to change the value.

Last edited by methyl; 02-02-2012 at 09:41 AM..
# 3  
Old 02-02-2012
Consider using the ksh's built-in "select" construct for constructing menus. This shows a nested example that does what you are looking for:
Code:
#!/usr/bin/ksh

#  Set select construct prompts for each menu level.
typeset -r MAINPROMPT="Select a main option: "
typeset -r MANGOPROMPT="Select mango option: "

#  Loop main menu until user exits explicitly.
while :
do
  print "\nMenu Title Goes Here\n"
  PS3=$MAINPROMPT  # PS3 is the prompt for the select construct.

  select option in mango tango rango exit
  do
    case $REPLY in   # REPLY is set by the select construct, and is the number of the selection.
      1) # mango
         #  Loop mango menu until user exits explicitly.
         while :
         do
           print "\nmango sub-menu title\n"
           PS3=$MANGOPROMPT
           select option1 in add substract exit
           do
             case $REPLY in
               1) # add
                  print "\nYou picked [add]"
                  break  #  Breaks out of the select, back to the mango loop.
                  ;;
               2) # subtract
                  print "\nYou picked [subtract]"
                  break  #  Breaks out of the select, back to the mango loop.
                  ;;
               3) # exit
                  break 2  # Breaks out 2 levels, the select loop plus the mango while loop, back to the main loop.
                  ;;
               *) # always allow for the unexpected
                  print "\nUnknown mango operation [${REPLY}]"
                  break
                  ;;
             esac
           done
         done
         break
         ;;
      2) # tango
         ;&  # Fall through to #3
      3) #rango
         print "\nYou picked $option"
         break
         ;;
      4) # exit
         break 2  #  Break out 2 levels, out of the select and the main loop.
         ;;
      *) # Always code for the unexpected.
         print "\nUnknown option [${REPLY}]"
         break
         ;;
    esac
  done
done

exit 0


Last edited by gary_w; 02-02-2012 at 11:51 AM.. Reason: added some comments, and fixed a cpl of bugs
# 4  
Old 02-02-2012
not the most efficient way but it works in showing user input with case statement.

Code:
#!/bin/bash

y=1
echo -e "Choose Names as listed in the format below.\n \
1. mango\t2. tango\t3. rango\t4. exit"
read answer


while [ $y=1 ]; do
    case ${answer} in
    "[Mm][Aa][Nn][Gg][Oo]"|1)
        echo "mango"
        mode="mango"
        echo -e "Specify an Option\n1. add\t2. sub\t3. exit"
        read option
        case ${option} in
            "[Aa][Dd][Dd]"|1)
                echo "you chose add"
                ;;
            "[Ss][Uu][Bb]"|2)
                echo "you chose sub"
                ;;
            "[Ee][Xx][Ii][Tt]"|3)
                echo "goodbye"
                exit 0
                ;;
        esac
        ;;
    "[Tt][Aa][Nn][Gg][Oo]"|2)
        echo "tango"
        mode="tango"
            echo -e "Specify an Option\n1. add\t2. sub\t3. exit"
            read option
            case ${option} in
                    "[Aa][Dd][Dd]"|1)
                            echo "you chose add"
                            ;;
                    "[Ss][Uu][Bb]"|2)
                            echo "you chose sub"
                            ;;
                    "[Ee][Xx][Ii][Tt]"|3)
                            echo "goodbye"
                            exit 0
                            ;;
        esac
        ;;
    "[Rr][Aa][Nn][Gg][Oo]"|3)
        echo "rango"
        mode="rango"
                echo -e "Specify an Option\n1. add\t2. sub\t3. exit"
                read option
                    case ${option} in
                    "[Aa][Dd][Dd]"|1)
                                echo "you chose add"
                                ;;
                    "[Ss][Uu][Bb]"|2)
                            echo "you chose sub"
                            ;;
                    "[Ee][Xx][Ii][Tt]"|3)
                            echo "goodbye"
                            exit 0
                            ;;
        esac
        ;;
    "[Ee][Xx][Ii][Tt]"|4)
        echo "goodbye"
        exit 0
        ;;
    *)
        echo"Please enter a valid answer"
        ;;
    esac
    echo "would you like to continue?"
    read reply 
    if [ "$reply"="y" ]; then
        y=1
    else
        y=0
    fi
done

# 5  
Old 02-02-2012
New, improved version that clears the screen before displaying the menu:
Code:
#!/usr/dt/bin/dtksh

# set select construct prompts for each menu level.
typeset -r MAINPROMPT="Select a main option: "
typeset -r MANGOPROMPT="Select mango option: "

#  Loop main menu until user exits explicitly.
while :
do
   clear
  print "\nMenu Title Goes Here\n"
  PS3=$MAINPROMPT  # PS3 is the prompt for the select construct.

  select option in mango tango rango exit
  do
    case $REPLY in   # REPLY is set by the select construct,
                     # and is the number of the selection.
      1) # mango
         #  Loop mango menu until user exits explicitly.
         while :
         do
           clear
           print "\nmango sub-menu title\n"
           PS3=$MANGOPROMPT
           select option1 in add substract exit
           do
             case $REPLY in
               1) # add
                  print "\nYou picked [add]"
                  break  #  Breaks out of the select, back to the mango loop.
                  ;;
               2) # subtract
                  print "\nYou picked [subtract]"
                  break  #  Breaks out of the select, back to the mango loop.
                  ;;
               3) # exit
                  break 2  # Breaks out 2 levels, the select loop plus
                           # the mango while loop, back to the main loop.
                  ;;
               *) # always allow for the unexpected
                  print "\nUnknown mango operation [${REPLY}]"
                  break
                  ;;
             esac
           done
         sleep 2
         done
         break
         ;;
      2) # tango
         ;&  # Fall through to #3
      3) #rango
         print "\nYou picked $option"
         break
         ;;
      4) # exit
         break 2  #  Break out 2 levels, out of the select and the main loop.
         ;;
      *) # Always code for the unexpected.
         print "\nUnknown option [${REPLY}]"
         break
         ;;
    esac
  done
sleep 2
done

exit 0

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash read input in case statement not working as expected

I'm having an issue with bash read input when using a case statement. The script halts and doesn't read the input on the first loop. if I hit enter then the scripts starts to respond as expected. Need some help here. defaultans=8hrs read -e -i $defaultans -p "${bldwht}How long would you like... (5 Replies)
Discussion started by: woodson2
5 Replies

2. Shell Programming and Scripting

Case statement in UNIX shell script

have written the below code to check whether the string received from user is a file name or dir using case statement, but its going into default case*). #!/bin/sh #Get a string from user and check whether its a existing filename or not rm str2 rm str3 echo "enter a file \c" read fil... (8 Replies)
Discussion started by: Mohan0509
8 Replies

3. Homework & Coursework Questions

How to read user keyboard input inside the case?

I need to Write a shell script that allows some system-administration tasks to be preformed automatically from a menu-driven interface. with automated following tasks: Copy directory tree Delete files or directories Output Information (this part is done ) *Copy directory tree The “Copy... (2 Replies)
Discussion started by: femchi
2 Replies

4. Shell Programming and Scripting

having some trouble in case statement

Hi All, I have script which will take the parameter as PNLYYYYMMDD/PNL or PNL-AMDYYYYYMMDD/PNL-AMD now based on YYYYMMDD my script will parse and do some logic within my case statement looks like below case `echo $1 | tr '' ''` in pnl*) do some logic;; pnl-amd*) do some logic;;... (2 Replies)
Discussion started by: manas_ranjan
2 Replies

5. Shell Programming and Scripting

Case Contruct Problem user input

This is supposed to be a simple bank script. Whenever I try the case construct options it wont work for the deposit option after typing the amount it just goes straight back into the menu, same with withdrawal option. Option 3 which should just display the balance amount doesn't echo anything. The... (2 Replies)
Discussion started by: jcnewton13
2 Replies

6. Shell Programming and Scripting

How to ignore case(upper and lower) from user input?

Hi, In my script it takes the input from the user. i.e. sys or system. How can i make it case insensitive in my code. if || ; then echo "valid" Any help is appreciated. Thanks, priya (4 Replies)
Discussion started by: priya001
4 Replies

7. Shell Programming and Scripting

!!VERY URGENT!! Trouble in getting user input, while using under CASE statement in UNIX

i want to get user input like this please tell which option to chose 1. mango 2. tango 3. rango if user chooses mango then it should execute a set of statements and again ask like this what do you want to do 1.add 2.subtract 3.exit when i choose exit it should goto my previous... (1 Reply)
Discussion started by: s.deepak
1 Replies

8. Shell Programming and Scripting

redirect input within case statement?

I'm trying to run the logic below but get a `<' is not matched error message when I return a Y or y; printf "Run this ? : " read RESP case $RESP in Y|y) cat <<EOF > file today is Monday EOF ;; N|n) exit 1 ;; esac Any ideas? (2 Replies)
Discussion started by: h8mmer
2 Replies

9. Shell Programming and Scripting

To ignore user input case.

hi, i will like to know whether awk command can ignore case? i written a script that will take in user input and search for data on the 1st field from a text file. echo -n "Title:" read title awk -F":" '$1~/'"$title"'/{print $0}' Filename read ans return ... (5 Replies)
Discussion started by: Cheranime
5 Replies

10. UNIX for Dummies Questions & Answers

case statement in UNIX scripting (ksh)

Hi, I have a script like below : #!/bin/ksh echo "Do you want to export all docs ?" read alld echo "Do you want to export template or report only " read temr case && ] #arguments ;; case && ] #arguments ;; case && ] #arguments ;; (4 Replies)
Discussion started by: luna_soleil
4 Replies
Login or Register to Ask a Question