0

I am a beginner at unix and I am trying to use a while loop to get a user integer input for 2 numbers, but I need to check if it is an integer and reask if it's not, so I attempted to utilize if statements inside the while loop. I cannot get this to work, what am I doing wrong with the while and if loop? #! /bin/bash

    echo “Enter first number:“

    while read number1
    do
      if[[ $number1 ]] && [ $input -eq $input 2>/dev/null ]
      then 
        echo “$number1”

      else
        echo "$number1 is not an integer or not defined.Try again”
      fi
     done


    echo “Enter second number:“
    while read number2
       do
        if[[ $number2 ]] && [ $input -eq $input 2>/dev/null ]
            then 
              echo “$number2”
        else
              echo "$number2 is not an integer or not defined.Try again”
         fi
    done
2
  • Please include in your question the mandatory four: ① What did you try, ② what did you expect to happen, ③ what did you observe instead, and ④ why is this not matching your expectation. You provided ① and ②. Commented Apr 7, 2020 at 18:36
  • Check your spaces (always around [[ and ]]) and your quotes. Commented Apr 7, 2020 at 20:50

1 Answer 1

1

If you are trying to get the number and checking it until it becomes integer, Please try the below code.

#!/bin/bash
echo "enter number"
while read number1
do
if ! [[ $number1 =~ ^[0-9]+$ ]]
then
        echo "number is not an integer"

else
        echo "number is an integer"
        exit;
fi
done
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.