Jump to content
 English      
HP.com Home Products and Services Support and Drivers Solutions How to Buy
» Contact HP
     Forums advanced search
HP.com Home
IT Resource Center Forums > HP-UX > general

Problems with or operator

» 

IT Resource Center

» Login
» Register
» My profile
» Search knowledge base
» Forums
» Patch database
» Download drivers, software and firmware
» Warranty check
» Support Case Manager
» Software Update Manager
» Training and Education
» More maintenance and support options
» Online help
» Site map

Member icons
 
 HP moderator  HP moderator
 Expert in this area  Expert in this area
Member status
ITRC Pro ITRC Pro
250 points
ITRC Graduate ITRC Graduate
500 points
ITRC Wizard ITRC Wizard
1000 points
ITRC Royalty ITRC Royalty
2500 points
ITRC Pharaoh ITRC Pharaoh
7500 points
Olympian Olympian
20000 points
1-Star Olympian 1-Star Olympian
40000 points
2-Star Olympian 2-Star Olympian
80000 points
»  How to earn points
»  Support forums FAQs
Question status
Magical answer Magical answer
Message with a response that solved the author's question
Favorites status
Add to my favorites Add to my favorites
Delete from my favorites Delete from my favorites
This thread has been closed Thread closed
 

Content starts here
   Create a new message    Receive e-mail notification if a new reply is posted  Reply to this message
Author Subject: Problems with or operator      Add to my favorites
suneeth
Nov 3, 2009 17:10:59 GMT   

Hi All,

I am new to unix and trying to create a shell script.

I have problems with the or operator in if condition.

if [ $1 != Trail ] -o [ $1 != Image ]

Is that a valid syntex for ksh ?
The condition checks Trail but not for Image. I also tried with || but doent work.

Please let me know
Thanks
Note: If you are the author of this question and wish to assign points to any of the answers, please login first.For more information on assigning points ,click here


Sort Answers By: Date or Points
Patrick Wallek Expert in this area This member has accumulated 40000 or more points
Nov 3, 2009 17:24:57 GMT    Unassigned

Do this instead:

if [ $1 != Trail -o $1 != Image ]
OldSchool This member has accumulated 7500 or more points
Nov 3, 2009 18:43:46 GMT  9 pts

Something appears wrong with the logic as well. Shouldn't that *always* come out a true?

if $1=Trial then the !=Image is true, if $1=Image the !=Trail is true, if it's anything else they both return true....

or do I need more coffee????
Patrick Wallek Expert in this area This member has accumulated 40000 or more points
Nov 3, 2009 19:01:59 GMT  5 pts

>>or do I need more coffee????

No OldSchool, you've got it correct as far as I can tell. Once I looked at it closer I realized the same as you.
James R. Ferguson This member has accumulated 80000 or more points
Nov 3, 2009 19:43:03 GMT    Unassigned

Hi:

You want an 'and' in this case:

# if [ "$1" != "Trail" -a "$1" != "Image" ]; then
...

Double quoting your variables also prevents syntax errors if they are ever undefined, too.

Regards!

...JRF...
OldSchool This member has accumulated 7500 or more points
Nov 3, 2009 20:50:26 GMT    Unassigned

"You want an 'and' in this case:

# if [ "$1" != "Trail" -a "$1" != "Image" ]; then"

Well....maybe.

You can take some actions / set of actions if $1 is not "Trail" and is not "Image", else do something else (optional).

This is the case with the test above. Had $1 been "Junk" it would have fallen thru / hit the else clause if present.

Another possibility is: you can take some actions / set of actions if $1 is *either* "Trail" or "Image", else do something else. in that case "if [ "$1" = "Trail" -o "$1 = "Image" ] ; then" would be appropriate

Depends on the OPs intent was. Since the basic logic posted was wrong, then either could apply, although I suspect that JRF has right.

You can make other cases by negating the logic to force it thru the "then" else, instead of the "else" clauses, but I'll not muddy the waters further
OldSchool This member has accumulated 7500 or more points
Nov 3, 2009 21:11:16 GMT    Unassigned

uh...that should have read:

"You can make other cases by negating the logic to force it thru the "then" CLAUSES, instead of the "else" clauses, but I'll not muddy the waters further
Dennis Handly Expert in this area This member has accumulated 40000 or more points
Nov 3, 2009 23:53:01 GMT    Unassigned

>I also tried with || but doesn't work.

If you use [[ ]], you can use || and &&.
if [[ "$1" != Trail && "$1" != Image ]]; then

You can also use the shell's && or ||:
[ "$1" != Trail ] && [ "$1" != Image ] && some-command

If you have just numeric values, you can C syntax:
if (( var1 != 99 && var1 != 88 )); then
suneeth
Nov 4, 2009 09:03:43 GMT    N/A: Question Author

Hi All,

Happy to see many replies overnight.
Anyway. Thanks for the replies

This works finally..
if [[ "$1" != Trail && "$1" != Image ]]

The intention was I want the parameter either Trail or Image. If it is trail do something or Image do something. I thought `and` will always check both conditions.

I tried all the above solutions but getting error. However still -a should work on strings. What is the syntax using the -a? I tried all the combinations.

Thanks
Dennis Handly Expert in this area This member has accumulated 40000 or more points
Nov 4, 2009 10:21:05 GMT  10 pts

>I thought AND will always check both conditions.

Not if the first is false.

>I tried all the above solutions but getting error.

What's your error?

>However still -a should work on strings.

Yes -a works on boolean conditions.
If $1 isn't Trail nor Image, it echoes bad #.
if [ "$1" != "Trail" -a "$1" != "Image" ]; then
   echo "bad 1"
fi
if [[ "$1" != Trail && "$1" != Image ]]; then
   echo "bad 2"
fi
[ "$1" != Trail ] && [ "$1" != Image ] && echo "bad 3"
suneeth
Nov 4, 2009 11:34:23 GMT    N/A: Question Author

Understand now.

Getting Syntax error at line 11 : `-a' is not expected for the below code

if [ "$1" != "Trail" -a "$1" != "Image" ];
if [[ "$1" != Trail && "$1" != Image ]];

Thanks
Suneeth
suneeth
Nov 4, 2009 11:41:39 GMT    N/A: Question Author

Sorry Dennis,

All of them are working. I missed to take a comment on one of the condition.

Thanks for all.
OldSchool This member has accumulated 7500 or more points
Nov 4, 2009 13:54:32 GMT    Unassigned

"This works finally..

if [[ "$1" != Trail -a "$1" != Image ]]

The intention was I want the parameter either Trail or Image. If it is trail do something or Image do something. I thought `and` will always check both conditions."


uh... in that case, I'd have thought something like

if [[ "$1" = Trail -o "$1" = Image ]]

would have been more appropriate, as you seem to be stating you want $1 to be either Trail or Image.

The test you posted will NOT run anything if $1 is "Trail" or "Image" but would run if $1 were, say, "Junk"
OldSchool This member has accumulated 7500 or more points
Nov 4, 2009 14:08:01 GMT    Unassigned

here's Dennis' test case modified to match the stated goal of:

"The intention was I want the parameter either Trail or Image. If it is trail do something or Image do something. I thought `and` will always check both conditions."


# if $1 is Trail or Image, it echoes found #.
echo $1
if [ "$1" = "Trail" -o "$1" = "Image" ]; then
echo "found 1"
fi
if [[ "$1" = Trail || "$1" = Image ]]; then
echo "found 2"
fi
[ "$1" = Trail ] || [ "$1" = Image ] && echo "found 3"


one of the many answers above should match what you are attempting to do. But since you state one case, then choose the logic for the other, it's really tough to tell what the heck you really want
suneeth
Nov 5, 2009 09:16:07 GMT    N/A: Question Author

uh... in that case, I'd have thought something like

if [[ "$1" = Trail -o "$1" = Image ]]

would have been more appropriate, as you seem to be stating you want $1 to be either Trail or Image.

The test you posted will NOT run anything if $1 is "Trail" or "Image" but would run if $1 were, say, "Junk"
-------------------------------

This is where i used my condition. Did i construct the code badly ?

if [ "$1" != "Trail" -a "$1" != "Image" ]; then
echo "Check the case"
exit
fi
#Then I start with statements for the parameter
while [ $1 == Trail ]
do
<statements>
done

while [ $1 == Image ]
do
<statements>
done
Dennis Handly Expert in this area This member has accumulated 40000 or more points
Nov 5, 2009 10:26:42 GMT    Unassigned

>Did I construct the code badly?
if [ "$1" != "Trail" -a "$1" != "Image" ]; then
   echo "Check the case"
   exit
fi

This seems fine.

>#Then I start with statements for the parameter
while [ "$1" == Trail ]; do

I'm not sure why you want a while, instead of just an if.  Do you use shift to get the next parm?

You may want to use a case:

case "$1" in
Trail) ... ;;
Image) ... ;;
*)  echo "Check the case"
    exit
    ;;
esac
suneeth
Nov 5, 2009 14:37:35 GMT    N/A: Question Author

Thanks a lot.
There is no loop in my code hence case is the right one.

I can cover this in if, but I will have some more conditions to check and I found this will be easier.
OldSchool This member has accumulated 7500 or more points
Nov 5, 2009 15:53:39 GMT    Unassigned

Suneeth,

My confusion revolved around your statement(s) like:

"The intention was I want the parameter either Trail or Image. If it is trail do something or Image do something. I thought `and` will always check both conditions."

-AND- the illustrated "IF" test

if [ "$1" != "Trail" -a "$1" != "Image" ]; then


After seeing your code snippet, I'd have said that your intention was to check if $1 was either "Trail" or "Image", and stop if not. This became clear when you posted some addtional code.


"if [ "$1" != "Trail" -a "$1" != "Image" ]; then
echo "Check the case"
exit
fi"


Actual code posted seems to be just fine. As Dennis noted, "case" may be a more elegant solution, or it have been written as series of if/elseif/elseif...statements.

In any event, sorry for the confusion.
suneeth
Nov 6, 2009 08:54:23 GMT    N/A: Question Author

Thanks for your comments.

As i stuck with If syntax i just want to know the whats issue is. As you said its always better to give more information instead of pasting it a single line code.

This will help others to give thier inputs correctly.
 
Create a new message    Receive e-mail notification if a new reply is posted   Reply to this message
 
 
Printable version
Privacy statement Using this site means you accept its terms
© 2009 Hewlett-Packard Development Company, L.P.