Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I test if a variable is *not* a positive integer?

Tags:

bash

To test whether a variable is e.g. a number greater than 0, you would write:

((i > 0))

But how would I test if the variable is not a number greater than 0?

EDIT

Sorry for the typo, meant not a number greater than 0.

EDIT 2

Sry for the poor question. The value of the variable is passed as a commandline argument, so it may be a number or not. This also needs to be checked.

like image 698
helpermethod Avatar asked Jun 05 '12 14:06

helpermethod


People also ask

How do you check if a number is positive or not?

If a number is greater than zero, it is a positive number. If a number is less than zero, it is a negative number. If a number equals to zero, it is zero.

Is a positive integer *?

Integers are all the whole numbers, both positive and negative.

How do you know if a variable is negative?

To check if a value is a negative number, compare it to 0 , e.g. num < 0 . If the value to the left hand side of the less-than operator is not already a number, it will get converted to one and compared to 0 . If the expression returns true , the value is a negative number.

How do you check if a variable is a positive integer Python?

Python Code: n = float(input("Input a number: ")) if n >= 0: if n == 0: print("It is Zero! ") else: print("Number is Positive number. ") else: print("Number is Negative number. ")

How to test whether a variable is a number or not?

To test whether a variable is e.g. a number greater than 0, you would write: But how would I test if the variable is not a number greater than 0? Sorry for the typo, meant not a number greater than 0. Sry for the poor question. The value of the variable is passed as a commandline argument, so it may be a number or not.

How to check if a variable is an integer or string?

You can refer to the below screenshot to check if the variable is an integer. In python, to check if the variable is a string, we will use built-in function isinstance () which will return a boolean value whether a variable is of type string or not.

How can I validate if a variable is an integer?

However, if you are trying to test whether or not the number is actually an integer before performing the comparsion, then you can use a compound expression like this to validate and compare a variable named int:

Why do we not use if statements with negative numbers?

So we know it returns 0 when it is a negative number, it returns 1 when it is zero, returns 2 when it is a positive number. So to not use if statements we store at 0th index “negative”, 1st index “zero” and at 2nd index “positive”, and print according to it.


3 Answers

i=15
if [[ ! $i -gt 10 ]]; then
        echo "lalala"
fi
like image 58
Daniel Kamil Kozar Avatar answered Oct 20 '22 19:10

Daniel Kamil Kozar


For checking on not being number, you have to test with regex. I have used -5 instead 0 just to demonstrate more general case (My assumption is that you use integers):

#!/bin/bash

A=$1

if [[ $A =~ ^[\-0-9]+$ ]] && (( A > -5)); then
  echo "A is number and is greater then -5"
else
  echo "A is not a number or is <= -5"
fi

If you want to test for non-integers, you have to clarify in your question what is considered number.

like image 22
Op De Cirkel Avatar answered Oct 20 '22 17:10

Op De Cirkel


Comparisons

This is clearly covered in the Bash manual sections on Shell Arithmetic and Bash Conditional Expressions.

# Shell arithmetic
$ (( -1 <= 0 )); echo $?
0

# Conditional expression
$ [[ -1 -le 0 ]]; echo $?
0

# Negated conditional. Confusing, but possible.
[[ ! -1 -gt 0 ]]; echo $?
0

Simple, right?

Validation

However, if you are trying to test whether or not the number is actually an integer before performing the comparsion, then you can use a compound expression like this to validate and compare a variable named int:

[[ "$int" =~ ^[-+]?([1-9][[:digit:]]*|0)$ && "$int" -le 0 ]]

Note that the comparison determines whether the validated integer is "less than or equal to zero," rather than negating the assertion that it is a positive integer. The entire expression will thus return true (e.g. an exit status of zero) only if the regular expression validates as a (possibly signed) integer and if the integer is a signed negative integer.

There are certainly other ways to construct this expression, but this is a fairly readable (if rather Bash-specific) solution.

Related Links

  • Bash: Testing if a variable is an integer
  • How can I tell whether a variable contains a valid number?
like image 35
Todd A. Jacobs Avatar answered Oct 20 '22 19:10

Todd A. Jacobs