Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
231 views
in Technique[技术] by (71.8m points)

scripting - 3 OR Conditions in Bash Statement

I would think this would be simple, but I keep getting errors. I want 3 OR conditions in my if else statement. These are the variations I've tried. What syntax error am I making?

   if [[ $var == "a" ]] || [[ $var == "b" ]] || [[ $var == "c" ]]; then
#Do Stuff
    else
#Do Stuff
    fi

    if [ $var == "a" ] || [ $var == "b" ] || [ $var == "c" ]; then
#Do Stuff
    else
#Do Stuff
    fi

    if [[ $var == "a" ] || [ $var == "b" ]] || [ $var == "c" ]; then
#Do Stuff
    else
#Do Stuff
    fi

    if [[ $var == "a"  || $var == "b" || $var == "c" ]]; then
#Do Stuff
    else
#Do Stuff
    fi
question from:https://stackoverflow.com/questions/65832829/3-or-conditions-in-bash-statement

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The first and last versions both look and act correctly for me. Are you sure you are using bash and not sh?

Note that [ and [[ are both "commands" (built-ins really, I think is the appropriate term) and are slightly different. Basically you want to use [[ unless you want legacy compatibility with old shells. If using [ there are additional considerations, for example it can cause problems with empty string comparisons.

Also as an aside, the 2nd version should generally work, but you get the problems mentioned above - and the 3rd version is not good.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...