Click to See Complete Forum and Search --> : Basic Bash Shell question


"Q"
09-20-2003, 01:58 PM
How do I get the shell script to jump back up and exicute code again.

I am trying to take a basic user imput and validate it.

To gain the data I am using "read"

Then running it into "case"

I have 3 options.

First two are appropiate data but the last one is * to pick up anything else and then prompt for the correct input.

My problem is of course that the script executes straight through. So how do I get it to go back up to prompt for the "read" again?

Thanks

matt

sclebo05
09-20-2003, 02:06 PM
you want to use a loop. there is a great bash shell scripting tutorial in the library section of this site. i'm not a great bash programmer, so you are better off checking there for syntax. but the basic idea is:

read info

while (info !=y || info !=n)
echo "Not a valid answer"
read info
loop

that is one way of doing it

"Q"
09-20-2003, 02:56 PM
Thanks for the repyl. That should work.

I would still like to know how to jump around the script by line number so I can start executing at a specific place or jump back up.

Or would this be done with functions?

sclebo05
09-20-2003, 03:13 PM
i think functions would be a better way of doing that. jumping around in code is sometimes called 'spaghetti code' and is generall frowned upon. :(

so use functions, and smile :D

"Q"
09-20-2003, 03:55 PM
Hey I got it too work

here is the correct syntax

read INFO

while [[ $INFO != "y" && $INFO != "n"]]
do
echo "pick y or n."
read INFO
done

Thanks for the pointer. I found out about the need for [[ from the tutorial on tldp and finally guessed about using && instaed of ||.

Thanks

Matt

sclebo05
09-20-2003, 04:27 PM
good call, i did tell you to use || instead of &&

sorry about that. glad you got it working though!