- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Problems with or operator
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-03-2009 09:10 AM
11-03-2009 09:10 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-03-2009 09:24 AM
11-03-2009 09:24 AM
Re: Problems with or operator
if [ $1 != Trail -o $1 != Image ]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-03-2009 10:43 AM
11-03-2009 10:43 AM
Re: Problems with or operator
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????
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-03-2009 11:01 AM
11-03-2009 11:01 AM
Re: Problems with or operator
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-03-2009 11:43 AM
11-03-2009 11:43 AM
Re: Problems with or operator
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-03-2009 12:50 PM
11-03-2009 12:50 PM
Re: Problems with or operator
# 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-03-2009 01:11 PM
11-03-2009 01:11 PM
Re: Problems with or operator
"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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-03-2009 03:53 PM
11-03-2009 03:53 PM
Re: Problems with or operator
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-04-2009 01:03 AM
11-04-2009 01:03 AM
Re: Problems with or operator
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-04-2009 02:21 AM
11-04-2009 02:21 AM
SolutionNot 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"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-04-2009 03:34 AM
11-04-2009 03:34 AM
Re: Problems with or operator
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-04-2009 03:41 AM
11-04-2009 03:41 AM
Re: Problems with or operator
All of them are working. I missed to take a comment on one of the condition.
Thanks for all.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-04-2009 05:54 AM
11-04-2009 05:54 AM
Re: Problems with or operator
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"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-04-2009 06:08 AM
11-04-2009 06:08 AM
Re: Problems with or operator
"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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-05-2009 01:16 AM
11-05-2009 01:16 AM
Re: Problems with or operator
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-05-2009 02:26 AM
11-05-2009 02:26 AM
Re: Problems with or operator
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-05-2009 06:37 AM
11-05-2009 06:37 AM
Re: Problems with or operator
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-05-2009 07:53 AM
11-05-2009 07:53 AM
Re: Problems with or operator
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-06-2009 12:54 AM
11-06-2009 12:54 AM
Re: Problems with or operator
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.