jmituzas
jmituzas

Reputation: 603

More Bash Choice Menu Options

How can I have more options to answer this?

while [ "$yn" != "Yes" ]; do
 echo "Please enter your Host Name"
 read hname
 echo "You have entered $hname, is this correct? (Yes or No)"
  read yn
done
sh runscript.sh

Would like to answer with "Yes" "yes" "Y" or "y", how can this be done? Thanks in Advance, Joe

Upvotes: 0

Views: 960

Answers (3)

Dennis Williamson
Dennis Williamson

Reputation: 360105

while [ -z "$yn" ]
do
    read -p "Please enter your Host Name" hname
    read -p "You have entered $hname, is this correct? (Yes or No)" yn
    case $yn in
        Y|y|Yes|yes);;    # this is a no-op
        *) unset yn;;
    esac
done

Upvotes: 6

jmituzas
jmituzas

Reputation: 603

Fixed it! Think this works best:

while [[ "$yn" != "Yes" && "$yn" != "Y" && "$yn" != "y" && "$yn" != "yes" ]]; do

Yay!

Upvotes: 0

salezica
salezica

Reputation: 76929

As pretty much any other language, Bash has logical operators. You can create something like this with Bash (this is pseudo-code):

If answer equals "Yes", or answer equals "yes":
   bla bla

I recommend you look up a Bash tutorial, you'll find that and much more! Cheers.

Upvotes: 1

Related Questions