dimanche 23 avril 2017

Bash: Multiple conditions with arithmetic, comparison and regular operators in if statement

I am trying to write a script that reads three integers, then checks if the sum of any two of those numbers is greater than the third one. If that is true, then it checks if those numbers are equal, and prints a message. If not, it checks whether any two of the numbers are equal and prints another message. If all of the above are false, it prints a message saying that all numbers are different. I have tried to put that in the following nested conditional:

read X
read Y
read Z
if [ $X + $Y > $Z ] && [ $X + $Z > $Y ] && [ $Y + $Z > $X ]
then
    if [ $X = $Y = $Z ]
    then
        echo "All numbers are equal."
    elif [ [ $X = $Y ] && [ $X != $Z ] ] || [ [ $X = $Z ] && [ $X != $Y ] ] || [ [ $Z = $Y ] && [ $X != $Y ] ]
    then
        echo "Two of the numbers are equal."
    else
        echo "None of the numbers is equal to another."
    fi
fi

I have tried all types of combinations with brackets and parentheses (the above is just one of them), but none of them has worked so far. I have already taken a look at related posts:

Multiple Conditions If Statement Bash Script

Bash: Two conditions in if

How to represent multiple conditions in a shell script?

but I haven't found any that are covering conditions with arithmetic operators in them. Can anyone please tell me what is the right way?

Aucun commentaire:

Enregistrer un commentaire