Guys,
I am trying to do below operation
LAST_TRANSACTION=2
if [[ $LAST_TRANSACTION != 1 || $LAST_TRANSACTION != 2 || $LAST_TRANSACTION != 3 ]]; then
# do something
fi
If the LAST_TRANSACTION variable is not equal to 1 or 2 or 3 then code inside the if block should be execute.
This code is not working, Any help is appreciated.
LAST_TRANSACTION=2 if [[ $LAST_TRANSACTION -ne 1 -o $LAST_TRANSACTION -ne 2 -o $LAST_TRANSACTION -ne 3 ]]; then # do something fi
flood
June 14, 2011, 8:27pm
3
To compare strings use "=" for equal and "!=" for not equal.
To compare numbers use "-eq" for equal "-ne" for not equal.
Still I get a problem
I am using below code, what ever might be the LAST_TRANSACTION value, code inside the if block is execute.
LAST_TRANSACTION=2
if [[ $LAST_TRANSACTION -ne 1 || $LAST_TRANSACTION -ne 2 || $LAST_TRANSACTION -ne 3 ]]; then
echo hi
fi
Since its a or condition and always it will come to If condition change it to and condition and try
Dude its an or condition so whatever may be the values it will go to if condition. Go for and condition
yazu
June 14, 2011, 9:43pm
7
if [[ $LAST_TRANSACTION -ne 1 && $LAST_TRANSACTION -ne 2 && $LAST_TRANSACTION -ne 3 ]]; then
or
if ! [[ $LAST_TRANSACTION -eq 1 || $LAST_TRANSACTION -eq 2 || $LAST_TRANSACTION -eq 3 ]]; then
you have used OR '||' operator instead of AND '&&'..so when it is checking the first condition that $LAST_TRANSACTION is not eqal to 1, it gets true and condition inside IF loop gets executed. :)