0

Trying to check whether or not a value entered by a user is an integer, as well as equal to 35 or 75. If the value entered is an integer and equal to 35 or 75, then I wish to inform the user of this. If the entered value is anything other than 35 or 75 (e.g. a string, null or different integer) then I aim to let the user know it's not valid and for them to try again.

read -p 'Please enter an integer that is either equal to 35 or equal to 75: ' value

if [ $value =~ ^[0-9]+$ ] && ([ $value -eq 35 ] || [ $value -eq 75 ]
    echo "The input is acceptable"
    exit 1

else
    echo "The value is invalid. Try again."

fi

exit 0

The error I keep receiving is line 10: syntax error near unexpected token 'else'

2
  • 1
    You're missing a closing parentheses. And the first condition should be wrapped in double brackets; single ones don't support regex. Commented Mar 22, 2021 at 12:01
  • @NigelWash: Complementing the correct commen of oguz ismail: Have a look at man test to see what is allowed within single brackets. Commented Mar 22, 2021 at 14:17

2 Answers 2

1

You were missing a then. I would tidy up like this:

#!/bin/bash

read -r -p 'Please enter an integer that is either equal to 35 or equal to 75: ' value

if [[ "$value" =~ ^[0-9]+$ ]] && [ "$value" -eq 35 ] || [ "$value" -eq 75 ]
    then
    echo "The input is acceptable"
    exit 0

else
    echo "The value is invalid. Try again."

fi

exit 0

Update: In order to keep prompting the user for input, do something along these lines:

#!/bin/bash

while 
  read -r -p 'Please enter an integer that is either equal to 35 or equal to 75: ' value
do
if [[ "$value" =~ ^[0-9]+$ ]] && [ "$value" -eq 35 ] || [ "$value" -eq 75 ]
    then
    echo "The input is acceptable"
    exit 0
else
    echo "The value is invalid. Try again."

fi
done

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

1 Comment

Hi Thomas, thanks a lot for the help! Is there a way to create a while loop that keeps prompting the user until the numbers 35 or 75 have been entered?
1

Not sure about the exit 1 when the answer is correct, I guess you wanted to do something like that:

#!/bin/bash

read -p 'Please enter an integer that is either equal to 35 or equal to 75: ' value

if [[ $value =~ ^[0-9]+$ && ($value -eq 35 || $value -eq 75) ]]; then
  echo "The input is acceptable"
else
  echo "The value is invalid. Try again."
  exit 1
fi

exit 0

I would personally simplify it to:

#!/bin/bash

read -p 'Please enter an integer that is either equal to 35 or equal to 75: ' value

if [[ "${value}" == "35" || "${value}" == "75" ]]; then
  echo "The input is acceptable"
else
  echo "The value is invalid. Try again."
  exit 1
fi

exit 0

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.