LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-10-2013, 11:35 PM   #1
joemon83
Member
 
Registered: Jan 2009
Posts: 42

Rep: Reputation: 0
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..

Last edited by joemon83; 07-10-2013 at 11:38 PM.
 
Old 07-10-2013, 11:48 PM   #2
mpapet
Member
 
Registered: Nov 2003
Location: Los Angeles
Distribution: debian
Posts: 538

Rep: Reputation: 71
Dash? Bash? Ksh? Zsh?

Which one?
 
Old 07-10-2013, 11:49 PM   #3
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 2,473

Rep: Reputation: Disabled
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?
 
Old 07-11-2013, 03:15 AM   #4
joemon83
Member
 
Registered: Jan 2009
Posts: 42

Original Poster
Rep: Reputation: 0
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.
 
Old 07-11-2013, 03:18 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,564

Rep: Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901
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'
 
Old 07-11-2013, 03:21 AM   #6
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_10{.0|.1|.2}
Posts: 4,520
Blog Entries: 6

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

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

Last edited by astrogeek; 07-11-2013 at 03:22 AM.
 
Old 07-11-2013, 03:34 AM   #7
joemon83
Member
 
Registered: Jan 2009
Posts: 42

Original Poster
Rep: Reputation: 0
Thanks Grail.
I should have used AND operator and not OR operator
 
Old 07-11-2013, 03:35 AM   #8
mddnix
Member
 
Registered: Mar 2013
Distribution: Redhat, Ubuntu
Posts: 525

Rep: Reputation: 141Reputation: 141
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" ]]

Last edited by mddnix; 07-11-2013 at 03:36 AM.
 
Old 07-11-2013, 03:44 AM   #9
joemon83
Member
 
Registered: Jan 2009
Posts: 42

Original Poster
Rep: Reputation: 0
Sorry guys, that was such a silly mistake. Thanks all for your help.
 
Old 07-11-2013, 04:16 AM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,564

Rep: Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901
Don't forget to mark as SOLVED once you have a solution.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] C++ Operator Overloading Within an Already Overloaded Operator mirlin510 Programming 8 04-17-2011 01:02 PM
What is the operator * ./. . . * royeo Linux - Newbie 2 09-03-2006 07:13 PM
redirection < operator not working pls help Fond_of_Opensource Linux - Newbie 3 08-25-2006 03:35 AM
(C++) vector assignment operator not working??? R00ts Programming 5 06-16-2005 12:39 PM
C++ operator += uman Programming 1 02-20-2005 05:37 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:50 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration