1

So basically I want to test if 3 variables are 0. If one of them is not it should report it. This is what I got:

        if [[ $result -ne 0 && $resultmax -ne 0 && $resultmin -ne 0 ]]
        then
            echo "There is something terribly wrong."
        fi

It does not work. Any idea where I messed up?

1

If you want to test that one of these variables is not 0, then you need || operator. Not &&.

$ if [[ 1 -ne 0 && 0 -ne 0 && 0 -ne 0 ]] ; then echo "There is something terribly wrong.";  fi

$ if [[ 1 -ne 0 || 0 -ne 0 || 0 -ne 0 ]] ; then echo "There is something terribly wrong.";  fi
There is something terribly wrong.
0

Now you are testing if all variables are not 0 to report error. Try:

if [[ $result -ne 0 || $resultmax -ne 0 || $resultmin -ne 0 ]]
then
    echo "There is something terribly wrong."
fi
0

To test if any of the variables is not 0 use the or operator || (as already suggested):

if [[ $result -ne 0 || $resultmax -ne 0 || $resultmin -ne 0 ]]
then
    echo "There is something terribly wrong."
fi

Though, if you're doing numerical computation and are using ksh (or bash, or zsh) you might prefer to use this syntax for clarity:

if (( result != 0 || resultmax != 0 || resultmin != 0 ))
then
    printf "%s\n" "There is something terribly wrong."
fi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.