dimanche 7 avril 2019

If statement not evaluated crrectly

I have a simple bash function that returns 3 numerical values: 0, 1, 2 When testing for the return value I get the correct value depending on the one returned from the function. # echo $? -> 0, 1, 2

However, when using an if-else statement the return value is not evaluate as I expected. For example when the function returns value = 2 in the if-else statement the elif [ $? -eq 1 ]; then is choosen

if [ "$?" -eq "0" ]; then
    echo "0"
elif [ "$?" -eq "1" ]; then
    echo "1"
elif [ "$?" -eq "2" ]; then
    echo "2"
else
    echo "Incorrect"
fi

Result: output is: 1 I expect: output is: 2

Any thoughts. Cheers, Roland

Aucun commentaire:

Enregistrer un commentaire