El Popo El Popo - 3 months ago 15
Bash Question

bash shell script case statement for two variables

I want to create a case statement including two expressions, my imagination is to look like this :

a=true
b=false

case [ "$a" || "$b"] in #<-- how can I do this with a case statement ?

true)echo "a & b are true" ;;
false)echo "a or b are not true" ;;

esac


Is it possible to do it with case instead of if ?

Thanks

Answer

This is an example but it's about strings, not real logical expressions:

$ cat > foo.sh
a=true
b=false
case $a$b in        # "catenate" a and b, two strings
    *false*)        # if there is substring false (ie. truefalse, falsetrue or falsefalse) in there
        echo false  # it's false
        ;;
    *)
        echo true   # otherwise it must be true
        ;;
esac

$ bash foo.sh
false
Comments