mary
mary

Reputation: 23

Shell scripting loop yes/no

Trying to run a script to ask a user a question with a yes/no answer. If yes generate a response and if no ask them the same question 4 times. I have tried many variations but keep failing. (I am completely new to scripting!)

read -p "Would you like a cup of tea?"
if [ "RESP" = "yes" ]; then
  echo "Great I will make you a cup of tea!"
else
  [ "RESP" = "no" ]: then
  echo [ "Are you sure you won't have a cup of tea?"
c=0
while [ $c -le 4 ]
count++
while [ $count -le 4 ]
fi

Upvotes: 1

Views: 2162

Answers (1)

Abhijit Pritam Dutta
Abhijit Pritam Dutta

Reputation: 5601

Your script is not correct please below script and try to understand how will it work. Now see your mistakes.

  1. You did not read the user response in a loop
  2. You did not read user response in a variable in your case RESP
  3. Both your while loops are not correct at all.

The below script will work for you. I will exit if you input yes and run for 4 times if you enter no or any other string. Or put a statement like invalid response for any value other than yes or no. Hope this will help you.

count=0
while [ $count -le 3 ]
do
    read -p "Would you like a cup of tea?" RESP
    if [ "$RESP" == "yes" ]; then
       echo "Great I will make you a cup of tea!"
       break
    else
        echo [ "Are you sure you won't have a cup of tea?"
        count=$((count+1))

    fi
done

Upvotes: 1

Related Questions