Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple bash scripting question (loops)

Status
Not open for further replies.

kmfdm515

Technical User
Jun 17, 2004
6
US
Hey,

I JUST started learning bash scripting and I'm having a bit of trouble. I'm trying to write a simple if-then loop that simply asks a question like 'Do you like pie?' and only accepts a 'yes' answer.

...so if you chose 'yes', it'd echo something like "good" and be done. If you chose 'no', it'd say "wrong answer" and ask again. If you said anything else it'd say "answer with yes or no" and ask again.

I can't seem to wrap my brain around how to do this with a 'while' loop - most examples I have are simply checking if one variable is true or false.

Thanks in advance...
 
Something like this ?
Code:
while read ans?"Do you like pie? "
do case $ans in
   yes) echo "good"; break;;
   no)  echo "wrong answer";;
   *)   echo "answer with yes or no";;
   esac
done

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Thank you very much! I'll keep at it....slowly but surely.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top