I am trying to get the user input of their details with the method inputInfo() every time the user press 1 at the menu page. the problem is after the first round of input. If I want to input another record of a person, it will keep printing out the pervious record.
1 Answer
Your while loops is valid after the initial case as the variables you read into are valid agaist your while loop conditions, so the prompt is never called as $name contains a valid value from the previous run.
inputInfo() {
nameRegex="[0-9]"
name=""
while [[ !$name =~ $nameRegex || -z $name ]]; do
echo "Enter name"
read name
if [[ $name =~ $nameRegex || -z $name ]]; then
echo "Name can only contain numbers or blank"
fi
done
You can just set the read variables to fail the conditions on the while loops. Setting them to zero values like the above will fix your issue.