Using && in if statement with 3 expressions

how do you write an if statement for something like

if ((expr 1 >= expr 2 && expr 3 >= expr 4) && expr 5 <= expr 6) 

if ((TRUE && TRUE) && TRUE) then
condition...

i've done it this way but it doesn't seem to work.

if ([[ "$ex_year" -ge "$curr_year"  && "$ex_month" -ge "$curr_month" ]]  && "$ex_day" -le "$curr_day" ); then
       condition...
if ([ $ex_year -ge $curr_year ] && [ $ex_month -ge $curr_month ]) && [ $ex_day -le $curr_day ]

You don't say what shell you're using and it makes a big difference. With a shell conforming to the standards, the following will work:

if [ "$ex_year" -ge "$curr_year" ] && [ "$ex_month" -ge "$curr_month" ] && [ "$ex_day" -le "$curr_day" ]
then    echo true
else    echo false
fi

If you're using a recent bash or ksh, the following will also do what you want:

if [[ "$ex_year" -ge "$curr_year" && "$ex_month" -ge "$curr_month" && "$ex_day" -le "$curr_day" ]]
then    echo true
else    echo false
fi
1 Like

Bash and ksh93 you can use (( )).

if ((   ex_year >= curr_year   &&  ex_month >= curr_month  && ex_day <= curr_day       ))
then
       condition...
fi

This work in every sh, dash, ksh, bash, = any posix-sh or old Bourne shell.

if [ "$ex_year" -ge "$curr_year" -a "$ex_month" -ge "$curr_month" -a "$ex_day" -le "$curr_day" ]
then 
    echo OK
fi

[ is test command and it include argument AND and OR = -a / -o

[
[[
((
are 3 different command.

cmd && cmd && cmd is different as [ sometest -a sometest -a sometest ] but in this case result is same.
=>
[ sometest ] && [ sometest ] && [ sometest ]
give same result as
[ sometest -a sometest -a sometest ]

1 Like