Operating System - HP-UX
1748242 Members
3970 Online
108759 Solutions
New Discussion юеВ

Problems with or operator

 
SOLVED
Go to solution
suneeth
Occasional Advisor

Problems with or operator

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


18 REPLIES 18
Patrick Wallek
Honored Contributor

Re: Problems with or operator

Do this instead:

if [ $1 != Trail -o $1 != Image ]
OldSchool
Honored Contributor

Re: Problems with or operator

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
Honored Contributor

Re: Problems with or operator

>>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
Acclaimed Contributor

Re: Problems with or operator

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
Honored Contributor

Re: Problems with or operator

"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
Honored Contributor

Re: Problems with or operator

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
Acclaimed Contributor

Re: Problems with or operator

>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
Occasional Advisor

Re: Problems with or operator

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
Acclaimed Contributor
Solution

Re: Problems with or operator

>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"