LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Change variable in case command (http://www.linuxquestions.org/questions/programming-9/change-variable-in-case-command-824787/)

New_2_Linux 08-07-2010 09:00 AM

Change variable in case command
 
Hi- first time posting a question...

I want to display 4 options using the case command and refresh the screen when options 1 and 2 are chosen (no changes to the options and you get asked again to chose option), but give a message for option 3 and exit on option 4. I set this up with the script below, but choosing option 1 works and choosing option 2 exits the script. How can get this to work? Thanks in advance for any assistance.

Code:

#!/bin/bash
#testing options

Option="0"

case $Option in
0|1|2) #display options again
clear
echo "Option- 1"
echo "Option- 2"
echo "Option- 3"
echo "Option- 4"
echo
echo -n "Enter your option: "
read Option
;;

3)
echo "You chose Option 3"
;;

4)
exit
esac


David the H. 08-07-2010 09:12 AM

I don't see how any of them could work. Since you're already inside the case structure when you issue the read command, setting the Option variable there would mean nothing to it. Not unless you used kind of loop to cycle back to the top again.

In your current set-up, $Option starts at 0, so the case command first clears the screen, then displays the menu, then reads in the input, then exits. And that's it.

New_2_Linux 08-07-2010 09:35 AM

Quote:

Originally Posted by David the H. (Post 4058938)
In your current set-up, $Option starts at 0, so the case command first clears the screen, then displays the menu, then reads in the input, then exits.

I've tried to set up case 1) and 2) to repeat the menu and re-read the input, but that exits the script. I'm really new to this so am trying to figure out how to display the menu of options and if options 1 and 2 are chosen nothing happens, but the menu still shows. Writing a case that repeats the menu seems redundant. How would I show the menu and have choice 1 and 2 show the menu? Maybe case isn't the way to go?

GrapefruiTgirl 08-07-2010 09:36 AM

Code:

#!/bin/bash
#testing options

Option="0" # Why quote this here? Not necessary, OR quote it below too on the case "$Option" in line.
          # Or, don't set it at all, because the `read` command will re-set it anyway.
echo
echo -n "Enter your option: "
read Option

case $Option in
 1|2)
  #display options again
  clear
  echo "You chose Option- $Option" ;;

 3)
  clear
  echo "You chose Option- 3" ;;

 4)
  # just exit if 4 is selected:
  exit ;;
 *)
  # test for anything other than 1,2,3 or 4
  echo "Unknown option. Use only 1,2,3 or 4" ;;
esac

Something more like the above will work better (untested - adjust as you need).

GrapefruiTgirl 08-07-2010 09:40 AM

And (since you updated post above) you might look at a `while` loop to test for options one and 2:

Code:

while [ ! "$Option" = "1" -a ! "$Option" = "2" ]; do
 clear
 echo -n "Please make a selection of 1,2,3 or 4"
 read Option
done

# now option is not 1 and not 2, so proceed with rest of code..

Note: I have used quotes above; if your input is to be considered integers, you'll have to account for this at some point, since comparing strings as integers and vice-versa, will lead you into troubles.

New_2_Linux 08-07-2010 10:11 AM

Quote:

Originally Posted by GrapefruiTgirl (Post 4058972)
you might look at a `while` loop to test for options one and 2

OK, let me play around and I'll get back to the forum. Thanks, both, for your helps so far!

David the H. 08-07-2010 05:09 PM

Good luck working it out. You'll get it eventually. It takes time to figure out stuff like this.

Realize that case is just a testing/branching structure. It's basically a variation on "if..else if..else". It doesn't do any looping or other feedback on it's own. One popular use is to embed it inside a while loop, so that you get a menu that refreshes on errors, and only exits when you tell it to.
Code:

while true; do    #loop indefinitely until broken.

    echo "Enter yes"
    read answer

    case $answer in

        yes) echo "Thank you.  Goodbye!"
              break
              #break exits the while loop.
        ;;

          *)  echo "That's not it.  Try again."
              #case exits without breaking the loop, so go back to the top.
        ;;
esac

done


konsolebox 08-07-2010 07:37 PM

the select command can also be an alternative.
Code:

PS3='Enter your option: '
select OPTION in 'Do nothing' 'Do nothing' 'Give a message' 'Exit'; do
    case "$OPTION" in
    'Do nothing')
        ;;
    'Give a message')
        echo 'You chose option 3.'
        ;;
    'Exit')
        break
        ;;
    esac
done



All times are GMT -5. The time now is 04:04 AM.