1

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
4
  • This probably helps you get started. Commented May 14, 2018 at 10:47
  • 1
    There are a number of errors and oddities in your script. It’d probably be better to read a shell scripting tutorial or book than to blindly try things and ask for ‘general help’ here. Commented May 14, 2018 at 10:48
  • Thanks for the feedback, I have been going through hours of tutorials and tried many variations which I have struggling with. Apologies I won't use this for feedback going forward if there is an issue with these type if queries. Commented May 14, 2018 at 11:16
  • It is awesome that you started programming. While it might feel intimidating and frustrating at times, remember that all the others felt the same. The important point is not to give up. Please do not be afraid to keep asking questions. Commented May 14, 2018 at 11:36

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

4 Comments

Note that $REPLY is used if no argument is supplied to read. Therefore "2." could also be interpreted as you should use $REPLY in the comparison.
If you are using a while loop with a manual count you could instead use a for loop: for ((count=0; count < 3; count++)) do echo $count; done.
While it is not mandatory, it is usually a good idea to use [[ instead of [ when using Bash.
Given that you're only using $count as loop control, I'd say for count in 0..3; do ...; done is a better approach here - then you don't need any arithmetic, and can stick to portable shell.

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.