Bash Logical Statement Question

I am fairly adept with bash but I have a general syntax question for an IF statement that has always seemed to elude me because it looks completely correct to me.

The code below works (tells me the compiler was successfully set):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

        local CC_TEST=${1}
        local CXX_TEST=${2}
        local NO_PATH=true
        if [ $# -eq 3 ]; then
            local CC_TEST=${3}/${1}
            local CXX_TEST=${3}/${2}
            local NO_PATH=false
        fi
        ############################
        if [ -x $CC_TEST -o $NO_PATH ]; then
            export CC=$CC_TEST
            echo "C Compiler is $CC"
        else
            echo "C Compiler \"$CC_TEST\" was not successfully set"
        fi


However if I switch NO_PATH to FULL_PATH and reverse the boolean values, e.g.

NO_PATH=true --> FULL_PATH=false

and change the evaluation

$NO_PATH to ! $FULL_PATH

the IF statement doesn't work (tells me the compiler was not successfully set), i.e.:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

        local CC_TEST=${1}
        local CXX_TEST=${2}
        local FULL_PATH=false
        if [ $# -eq 3 ]; then
            local CC_TEST=${3}/${1}
            local CXX_TEST=${3}/${2}
            local FULL_PATH=true
        fi
        ############################
        if [ -x $CC_TEST -o ! $FULL_PATH ]; then
            export CC=$CC_TEST
            echo "C Compiler is $CC"
        else
            echo "C Compiler \"$CC_TEST\" was not successfully set"
        fi


Can someone explain this to me? I don't believe it matters but I run this on Mac OS X, but to my understanding, it is the coreutils (sed, grep, etc.) that differ on OS X from Linux (i.e. FreeBSD vs. GNU) not the bash shell itself.
I think you have line 11 wrong. You need a conditional after the -o. At the moment you have a string which always evaluates true. The true and false are just strings with no special meaning.

I think you want:
if [ -x $CC_TEST -o "$NO_PATH" == "true" ]; then

Look in "Advanced Bash Scripting"
Last edited on
Topic archived. No new replies allowed.