shell - AND, OR conditions in if statement -
i have following code
elif [ $text2 == 'landing' ] && [ "$text4" == 'fail' ] || [ "$text4" == '' ];
the condition text2 should landing , text4 can either fail or null. how evaluate above command.
please if im doing wrong in advance
you need group them explicitly:
elif [ "$text2" = 'landing' ] && { [ "$text4" = 'fail' ] || [ "$text4" = '' ]; };
your attempt succeed either of following 2 conditions held:
text2
landing andtext4
failtext4
empty or unset.
since &&
, ||
have same precedence, (perhaps surprisingly) write without grouping:
elif [ "$text4" = '' ] || [ "$text4" = fail ] && [ $text2 == 'landing' ];
if using bash
, can use [[ ... ]]
command instead of [ ... ]
. grouping required; operators inside [[ ... ]]
do have precedences expect other languages (that is, a || b && c
a || (b && c)
, not (a || b) && c
).
elif [[ $text2 == 'landing' && ( "$text4" == 'fail' || "$text4" == '') ]];