Home > Software design >  Bash - complex if condition statement, combining && and || to test arithmetic and string together
Bash - complex if condition statement, combining && and || to test arithmetic and string together

Time:11-19

I am trying to test few things together. but my if statement is throwing an error saying:

./script1.sh: line 30: expected `)'
./script1.sh: line 30: syntax error near `0)'
./script1.sh: line 30: `  if [[ (($1 < 0) || ("$1" == "-0")) && ($3 >= 0) ]]'

my actual code is:

#!/bin/bash
sub(){      
if [[ (($1 < 0) || ("$1" == "-0")) && ($3 >= 0) ]]
then
 echo "Condition is true"
else
  echo "Condition is false"
fi
}
A=-0
B=50
C=-0
D=55
sub "$A" "$B" "$C" "$D"

can anyone suggest how to fix such issue?

CodePudding user response:

In bash, the following should work:

if [[ (($1 -lt 0) || ("$1" == "-0")) && ($3 -ge 0) ]]; then
    # Do something
else
    # Do something
fi

Are you sure you're running this with bash? Not all older shells support [[.

  • Related