vendredi 23 août 2019

Bash doesn't display the result of nested if-else statements

I'm trying to nest a couple of if/else statements. Maybe I'm making a very silly mistake but I can't get it to work. There are 3 variables in order, one of them at random will be "-1".

If one of them is "-1", then all the following ones are "-1" as well, so if FOO2 is "-1", then FOO3 is as well.

I need to print to the console whenever a variable is "-1", prioritizing the first one, so the last example should only print "FOO2 is -1" and nothing else.

This should print "FOO3 is -1", but it doesn't.

FOO1="2"
FOO2="2"
FOO3="-1"

if [ ${FOO1} -eq "-1" ]
then
    if [ ${FOO2} -eq "-1" ]
    then
        if [ ${FOO3} -eq "-1" ]
        then
            echo "FOO3 is -1"
        else 
            echo "FOO2 is -1"
        fi
    else 
        echo "FOO1 is -1"
    fi
fi

Aucun commentaire:

Enregistrer un commentaire