0

I'm trying to write a bash script that asks if a person wants a cup of tea, with Y returning “Great, I’ll make tea now” to the console"; and N returning “Are you sure” 4 more times before stopping the loop. Also if during the 4 follow up offers, the user changes their mind and presses ‘Y’, the computer will print out “Great, I’ll make tea now” to the console.

My script below results in an infinite loop, so I need some increment count in the code and I just cant figure out where? Possibly a very basic question and if so apologies, new to Linux Bash scripting, any help appreciated:

#!bin/bash
#tea2.sh
echo "Will you have a cup of tea?"
read answer
while [ "$answer" = n ]
do
  echo "Are you sure"
  if [ "$answer" = y ]
  then
        break
  fi
  echo "Great, I\’ll make tea now"
done

second script:

#!/bin/bash
#tea3.sh
set yname="Would you like a cup of tea?"
while ($yname != "n")
       echo -n "Are you sure?"
       set yname = $<5
       if ($yname != "y") then
              echo "Great, I\'ll make tea now"
fi
done
1
  • I think there's at least three syntax errors in the last script. Please at least fix those before posting, or reduce the script to a minimal example and post it as a separate question. Commented May 7, 2018 at 20:08

1 Answer 1

2

You're never asking inside the loop, so the answer will never change. Hence the infinite loop. What you want to do is read answer inside the loop (and Use More Quotes™).

By the way, a typographic quote like the one you're using has no special meaning in Bash, so it doesn't need to be escaped.

Sign up to request clarification or add additional context in comments.

2 Comments

thank you for the response, I have moved the read answer inside the loop and added double quotes to the echo "output" but I'm still getting the infinite loop. So I do not need an increment count within the loop? Also "a typographic quote like the one you're using has no special meaning in Bash, so it doesn't need to be escaped" does this mean I dont need the done command? Again sorry if these are basic Qs and appreciate any feedback
sorry I've updated the formatting and edited the Question, also added a new script which I'm attempting. The first script still results in an infinite loop, the second script doesnt like the done command for some reason but I'm hoping the rest is ok, feedbak appreciated

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.