False positive IF condition, in BASH. Testing ((A ||B ) && C)

40 Views Asked by At

Please see below example test code that I am getting false positive for:

#!/bin/bash
sub1(){      
if [[ (($1 -lt 0) || "$1" == "-0") && ($3 =~ re) ]]
then
 echo "Sub1: Condition is true $1 $2 $3 $4"
else
 echo "Sub1: Condition is false $1 $2 $3 $4"
fi
}

sub2(){      
if [[ (($1 -lt 0) || ("$1" == "-0")) && ($3 -ge 0) ]]
then
 echo "Sub2: Condition is true $1 $2 $3 $4"
else
 echo "Sub2: Condition is false $1 $2 $3 $4"
fi
}

nz='^-0$'
re='^[1-9]+$'
ne='^-[1-9]+$'
A=-0
B=50
C=-0
D=55
sub1 "$A" "$B" "$C" "$D"
sub2 "$A" "$B" "$C" "$D"

I am trying to pass multiple values of C (ex. 0, -0, 1, -1 etc) Function Sub1 I am trying to check conditions using regex. Function Sub2 I am trying to check conditions using arithmetic and string conditions. None of them gives error but both provides false positive when you test with all possible values of C.

Any idea what's wrong in If statement?

0

There are 0 best solutions below