0

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 1

1

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.

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

Comments

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.