LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   or operator not working (https://www.linuxquestions.org/questions/linux-newbie-8/or-operator-not-working-4175469227/)

joemon83 07-10-2013 10:35 PM

or operator not working
 
Can someone tell me why this doesn't work:

=============================
PHASE="upgrade";

if [ "$PHASE" != "install" -o "$PHASE" != "upgrade" ]
then
echo "Nothing to be done here"
else
echo "Proceed with the next step"
fi
=============================
It always prints "Nothing to be done here"
But if I remove the -o operator and the second condition, then it works.

Some issue with -o operator i guess..

mpapet 07-10-2013 10:48 PM

Dash? Bash? Ksh? Zsh?

Which one?

Ser Olmy 07-10-2013 10:49 PM

Take a close look at the logic of that test. "If $PHASE is either unlike "install" or unlike "upgrade", then there's nothing to be done."

Under what circumstances will $PHASE not be unlike at least on of the two conditions?

joemon83 07-11-2013 02:15 AM

Shell is bash.

@Ser Olmy: If you take a closer look you can see that the variable PHASE is already set to "upgrade".
So the if condition shouldn't match, but else condition should match.

grail 07-11-2013 02:18 AM

Actually, boolean logic for OR says that if the first test is true then the second never need be tested, hence, since PHASE is not equal to "install" is true the 'if' is executed and not the 'else'

astrogeek 07-11-2013 02:21 AM

If it is set to "upgrade" then it ALWAYS !="install" and correctly matches.

** grail types faster than I do... **

joemon83 07-11-2013 02:34 AM

Thanks Grail.
I should have used AND operator and not OR operator :)

Madhu Desai 07-11-2013 02:35 AM

Flawed logic. No matter what value PHASE has - 'install', 'upgrade' or any other 'rambo' value, it will always return TRUE.

Use instead
Code:

if [[ "$PHASE" == "install" || "$PHASE" == "upgrade" ]]

joemon83 07-11-2013 02:44 AM

Sorry guys, that was such a silly mistake. Thanks all for your help.

grail 07-11-2013 03:16 AM

Don't forget to mark as SOLVED once you have a solution.


All times are GMT -5. The time now is 02:04 PM.