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.
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.
Integers are all the whole numbers, both positive and 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.
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. ")
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.
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.
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:
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.
i=15
if [[ ! $i -gt 10 ]]; then
echo "lalala"
fi
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.
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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With