Beginner bash - basic shell script 'while' help...

Hi everyone, first time visitor to these forums here.

Keeping a long story short I've been attempting to learn how to code in bash. I have VERY little previous experience with coding languages besides simply copying and pasting batch scripts for Windows. So, with that in mind I've followed a few tutorials on how to use some basic commands and how to write and execute basic scrips in bash. I'm now trying to put what I've learned to the test and am attempting to write out an entire (admittedly simple and useless) script without following any kind of tutorial. Here's what I have so far:

#!/bin/bash
while true; do
        echo "What is your name?"
        read name
        echo "What is your age?"
        read age
        echo "What is your sex?"
        read sex
        echo "so you're a $age old $sex called $name, is that right?"
        read right
                if test $right = Yes
                then echo "Great, thanks!"
                elif test $right = No
                then echo "Please enter your information again."
        fi
done

It's just a simple script that asks the user to input their information, and confirm that the details provided are correct. What I'm trying to do is have it so that if the details are incorrect and the user inputs "No" after being asked to confirm their details, a message is displayed asking them to enter their information again, and then return to the beginning of the script. However I'm struggling with getting to grips with the "while" command. Currently it doesn't matter what the user inputs as an answer, the script will restart anyway.

I've attempted to look online at some "while" loop tutorials, but so far I'm either not looking in the right places or It's just going straight over my head right now.

Can anyone offer some advice? If anyone does I really would appreciate it, I know that this is incredibly dull and basic for most of you...

A quick fix would be to use break :

then 
  echo "Great, thanks!"
  break
elif ....

Which will end the loop when the user answers "Yes"

--
Or

right=No
while test $right != Yes
do
  ...
done

Excellent, "break" worked a treat! Many thanks.

Any suggestions as to where to find some decent tutorials on using "while" loops? I don't just want to throw a messy script together and call it quits, I actually want to learn how to do this properly?

Appreciate the quick reply though. :b:

Let me give you a few tasks that will help you experiment toward your goal as highlighted in read
Before you utilize any input validate it as something you were expecting.
e.g.
How would you validate that the user have entered a proper gender? What if the user enters "Neutral" or any other variation of modern gender answers? What if it just pressed the ENTER/Return key?
How would you validate an age? What if the user enters 159 or 0 or just the ENTER/Return key?
How would you validate a name? Would you accept "Shakespeare's Hamlet" inputted as string for a name?

Just an example for a multi-line while condition;
the last exit status before the do counts.

while
    echo "What is your name?"
    read name
    echo "What is your age?"
    read age
    echo "What is your sex?"
    read sex
    echo "so you're a $age old $sex called $name, is that right?"
    read right
    test "$right" != Yes
do
    if test "$right" = No
    then
        echo "Please enter your information again."
    fi
done
echo "Great, thanks!"
1 Like