ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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.
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.
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?
Last edited by New_2_Linux; 08-07-2010 at 10:19 AM.
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 -n "Enter your option: "
case $Option in
#display options again
echo "You chose Option- $Option" ;;
echo "You chose Option- 3" ;;
# just exit if 4 is selected:
# test for anything other than 1,2,3 or 4
echo "Unknown option. Use only 1,2,3 or 4" ;;
Something more like the above will work better (untested - adjust as you need).
And (since you updated post above) you might look at a `while` loop to test for options one and 2:
while [ ! "$Option" = "1" -a ! "$Option" = "2" ]; do
echo -n "Please make a selection of 1,2,3 or 4"
# 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.
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.
while true; do #loop indefinitely until broken.
echo "Enter yes"
case $answer in
yes) echo "Thank you. Goodbye!"
#break exits the while loop.
*) echo "That's not it. Try again."
#case exits without breaking the loop, so go back to the top.