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
Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137
mary
  • 23
  • 4
  • [This](https://stackoverflow.com/q/226703/4162356) probably helps you get started. – James Brown May 14 '18 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. – Biffen May 14 '18 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. – mary May 14 '18 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. – Micha Wiedenmann May 14 '18 at 11:36

1 Answers1

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
Abhijit Pritam Dutta
  • 5,521
  • 2
  • 11
  • 17
  • 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. – Micha Wiedenmann May 14 '18 at 11:29
  • 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`. – Micha Wiedenmann May 14 '18 at 11:31
  • While it is not mandatory, it is usually a good idea to use `[[` instead of `[` when using Bash. – Micha Wiedenmann May 14 '18 at 11:33
  • 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. – Toby Speight May 14 '18 at 16:19