Operating System - HP-UX
1748111 Members
3518 Online
108758 Solutions
New Discussion юеВ

Re: Problems with or operator

 
SOLVED
Go to solution
suneeth
Occasional Advisor

Re: Problems with or operator

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

Re: Problems with or operator

Sorry Dennis,

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

Thanks for all.
OldSchool
Honored Contributor

Re: Problems with or operator

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

Re: Problems with or operator

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

Re: Problems with or operator

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

done

while [ $1 == Image ]
do

done
Dennis Handly
Acclaimed Contributor

Re: Problems with or operator

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

Re: Problems with or operator

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

Re: Problems with or operator

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

Re: Problems with or operator

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.