1

This checks if argument is an integer in Bourne Shell Script:

if [[ $3 =~ ^[0-9]+$ ]] && ((  $3 >= 1 ))

How do I check if argument is not an integer (can consist of integers and alphabets)? So, I guess it's just the complement of above, but I'm not sure how to change it. Where can I find info on what these symbol mean?: =~ ^ + $ &

1 Answer 1

3

You can use De Morgan's Law to negate that if condition check as follows:

if [[ ! $3 =~ ^[0-9]+$ ]] || ((  $3 < 1 ))
  • The ! symbol inside [[...]] means negation.
  • $3 < 1 is the negation of $3 >= 1

The =~ operator allows use of Regular Expression in an if statement.

The && used here means "Logical AND"

The rest of the symbols ^ + $ are for Regular Expression, which is a topic deserving more reading than I can provide in this answer, but in short:

  • ^: match start of line
  • +: match one or more (in this context it matches one or more digits)
  • $: match end of line
  • Together, ^[0-9]+$ means: Only match a string of nothing but digits.
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.