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:

  1. text2 landing and text4 fail
  2. text4 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" == '') ]]; 

Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo