case loop... repeat on bad input?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting case loop... repeat on bad input?
# 1  
Old 02-18-2009
case loop... repeat on bad input?

I'm trying to get a case statement to start over if an undefined option is selected... But I am ata loss on how to actually do it.

Here is a quick example of what I have.

Echo "1) do this/n
2) Do that/n
3) Quit/n
Make a selection/n"
Read answer
Case answer in
1) Dothid;;
2) Dothat;;
3) Exit;;
*) restart the echo menu

Can anyone point me in the correct direction on how to do this... Ithink that I need a while loop but I am not sure how to structurew it.

Please ignore the sloppy example and typos, stuck on a blackberry
# 2  
Old 02-18-2009
here is an example using select. if you don't want to use select you can just write a loop around your existing code and "continue" if they don't select a valid option.

Code:
select f in foo bar blah
do

   case "$f" in 
       foo) echo foo
            break
       ;;
       bar) echo bar
            break
       ;;
       blah) echo blah
            break
       ;;
       *) echo sorry
    esac

done

# 3  
Old 02-19-2009
Quote:
Originally Posted by trey85stang

Echo "1) do this/n
2) Do that/n
3) Quit/n
Make a selection/n"
Read answer
Case answer in
1) Dothid;;
2) Dothat;;
3) Exit;;
*) restart the echo menu

Please ignore the sloppy example and typos, stuck on a blackberry
Are u goin to make selection as only 1,2 or 3 or is it full line..
Suppose that u have to make selection as 1) do this then do the following :

Code:
case $answer in 
                 "1\) do this")
                                   echo Dothis
                  ;;
                 "2\) do that")
                                   echo Dothat
                  ;;
                 "3\) quit")
                                   echo exit
                  ;;
                 *) restart the echo menu
                  ;;
esac

I dont have access to unix server right now sop dint try out my code. Please let me know whether it works.
# 4  
Old 02-19-2009
frank, changing everything to select worked perfectly for my needs. Thank you very much!
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

How to use xargs to repeat as a loop to grab date string?

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: My goal to find how many requests in 14 days from weblog server. I know to cat a weblog file to wc -l to find the... (8 Replies)
Discussion started by: scopiop
8 Replies

2. Homework & Coursework Questions

How to use loop to repeat task?

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. How can i use loop to repeat task. 2.shirt=15 black=13.50 echo "how many shirt you want" read num echo echo "Please enter a choice" echo "1 ---> normal... (5 Replies)
Discussion started by: Tauatioti
5 Replies

3. Shell Programming and Scripting

Syntax error: Bad for loop variable

I'm getting an error while running this script. Need help. set -x verbose #echo on clear #clear the screen USERNAME="bbb" PASSWORD="password" SERVER="192.168.1.100" WAIT_TIME=300 FILE_PATH="/home/users/xxx/MMM" # local directory to pickup *.dat file REMOTE_PATH="/Drop_off/xxx/yyy" #... (17 Replies)
Discussion started by: clgz2002
17 Replies

4. Shell Programming and Scripting

Repeat using for loop

I have a file like this 2011-10-10 10:46:00,1-1-13-1-1,151510,ALCLA0A84D2C 2011-10-10 10:46:00,1-1-13-1-1,151520,65537 2011-10-10 10:46:00,1-1-13-1-1,151515,46932 2011-10-10 10:46:00,1-1-13-1-1,151521,32769 2011-10-10 10:46:00,1-1-13-1-1,151522,32769 2011-10-10... (4 Replies)
Discussion started by: LavanyaP
4 Replies

5. 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

6. Shell Programming and Scripting

Accepting Input regardless of Case

Hi I am trying to get my script to accept input regardless if the person enters a or A. here is the portion of the code where I get the input. echo -n 'Please enter your choice:' # prompt user for input. read reply # read input echo case $reply in #... (2 Replies)
Discussion started by: DualPandas
2 Replies

7. Shell Programming and Scripting

Syntax error: Bad for loop variable

Hi Can any one help, I'm trying to run a script that beeps out the ip address from the PC internal speaker with the following script. It keeps throwing the error "Syntax error: Bad for loop variable" on line 16. I know its picking up the IP ADDRESS correctly. Any ideas on whats wrong. I'm... (3 Replies)
Discussion started by: dman
3 Replies

8. 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

9. Shell Programming and Scripting

using case to do this might be a bad idea?

Reading this file. I want to read all 4 fields. If 2 is populated with a p, I want to set $TYPEP to "Printers", if not should be empty. If 3 is populated with an r, I want to set $TYPER to "REQ Printers" if not should be empty. If 4 is populated with letter o, I want to set $TYPEO to... (12 Replies)
Discussion started by: Skyybugg
12 Replies
Login or Register to Ask a Question