LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 08-07-2010, 09:00 AM   #1
New_2_Linux
LQ Newbie
 
Registered: Oct 2009
Posts: 6

Rep: Reputation: 0
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

Last edited by New_2_Linux; 08-07-2010 at 09:08 AM.
 
Old 08-07-2010, 09:12 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
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.
 
Old 08-07-2010, 09:35 AM   #3
New_2_Linux
LQ Newbie
 
Registered: Oct 2009
Posts: 6

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by David the H. View Post
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.
 
Old 08-07-2010, 09:36 AM   #4
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 542Reputation: 542Reputation: 542Reputation: 542Reputation: 542Reputation: 542
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).
 
Old 08-07-2010, 09:40 AM   #5
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 542Reputation: 542Reputation: 542Reputation: 542Reputation: 542Reputation: 542
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.
 
Old 08-07-2010, 10:11 AM   #6
New_2_Linux
LQ Newbie
 
Registered: Oct 2009
Posts: 6

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by GrapefruiTgirl View Post
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!
 
Old 08-07-2010, 05:09 PM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
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
 
Old 08-07-2010, 07:37 PM   #8
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,242
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
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
 
1 members found this post helpful.
  


Reply

Tags
case, command


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash: change case graemef Programming 9 04-06-2010 08:48 PM
bash : change first character to upper case powah Programming 5 07-18-2007 08:18 PM
Change Case DavidPhillips Programming 6 04-02-2005 04:45 PM
change case in perl? bigearsbilly Programming 3 01-20-2005 07:08 AM
Very complex for newbie, why in some case I can set variable and in other - I can't beep_beep Linux - General 2 09-25-2002 08:41 AM


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

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration