- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Shell Programming
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
06-19-2003 09:43 AM
06-19-2003 09:43 AM
I am attempting to ensure that the value of a given variable matches one of three strings.
Basically it's
if "$PIE" does not equal "cherry" "apple" or "pumpkin" then i want to exit the loop and print the usage statement. If it does match then i want the loop to continue.
My syntax is as follows but does not work, I think is may be an -0 or || compund expression issue.... Thanks.
#!/usr/bin/sh
PIE=cherry
if [ "$PIE" != [ "apple" || "cherry" || "pumpkin" ]]
then
echo "\n****Valid names are apple,cherry or pumpkin!\n" ; exit 1
else
echo "\n****Name is valid!\n"
Thanks in advance for the help....
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-19-2003 09:48 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-19-2003 09:52 AM
06-19-2003 09:52 AM
Re: Shell Programming
...
# if [ "$PIE" != "apple" -a "$PIE" != "cherry" -a "$PIE" != "pumpkin" ]; then
> echo "bad pie!"
> else
> echo "yummy pie!"
> fi
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-19-2003 09:52 AM
06-19-2003 09:52 AM
Re: Shell Programming
I would probably do that with "case":
case $PIE in
pumpkin) print "Thank you.";;
cherry | apple) print "Gracias.";;
*) print "Wrong answer. Sorry..."
exit;;
esac
# Process pie
statements...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-19-2003 10:00 AM
06-19-2003 10:00 AM
Re: Shell Programming
Try the following:
if [ "$PIE" != "apple" -o "$PIE" != "cherry" -o "$PIE" != "pumpkin" ]
then
echo "Not good"
else
echo "Goood!"
fi
Regards,
Dario
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-19-2003 10:01 AM
06-19-2003 10:01 AM
Re: Shell Programming
if [ $PIE = apple -o $PIE = cherry -o $PIE = pumpkin ]
then
echo true
else
echo wrong
fi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-19-2003 10:11 AM
06-19-2003 10:11 AM
Re: Shell Programming
this is a logic problem.
The logic says
"If $PIE is not equal to "apple" OR if $PIE is not equal to "cherry" OR if $PIE is not equal to "pumpkin"
then
echo "\n****Valid names are apple,cherry or pumpkin!\n" ; exit 1
else ...
As written, $PIE is not equal to "apple" so the test is TRUE and because it is TRUE, the shell will not evaluate the rest of the tests and it executes the first echo command.
The way to do this is by using an AND operator.
Here is a version that will do what you want.
#!/bin/sh
PIE=cherry
if [ "$PIE" != "apple" -a $PIE != "cherry" -a $PIE != "pumpkin" ]
then
echo "\n****Valid names are apple,cherry or pumpkin!\n" ; exit 1
else
echo "\n****Name is valid!\n"
fi
HTH,
Michael.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-19-2003 10:18 AM
06-19-2003 10:18 AM
Re: Shell Programming
Do *not* [no pun intended] use the 'or' operator [ -o ] with negatated equality checks. You will not achieve the desired outcome. Try this:
# PIE=apple
# [ "$PIE" != "apple" -a "$PIE" != "cherry" -a "$PIE" != "pumpkin" ] && echo BAD_PIE!
...now change '-a' to '-o':
[ "$PIE" != "apple" -o "$PIE" != "cherry" -o "$PIE" != "pumpkin" ] && echo BAD_PIE!
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-19-2003 10:25 AM
06-19-2003 10:25 AM
Re: Shell Programming
if [ "$PIE" != "apple"] || [ $"PIE" != "cherry" ] || [ "$PIE" != "pumpkin" ] ;
then
echo "Usage: `basename $0` {whatever options}"
exit
else
#whatever commands you wnat
fi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-19-2003 10:29 AM
06-19-2003 10:29 AM
Re: Shell Programming
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-19-2003 10:38 AM
06-19-2003 10:38 AM
Re: Shell Programming
Null points pse. Ian
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-19-2003 10:44 AM
06-19-2003 10:44 AM
Re: Shell Programming
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-19-2003 10:45 AM
06-19-2003 10:45 AM
Re: Shell Programming
Your example should be use "&&" not "||" as:
# if [ "$PIE" != "apple" ] && [ "$PIE" != "cherry" ] && [ "$PIE" != "pumpkin" ]
> then
> echo BAD_PIE
> else
> echo GOOD_PIE
> fi
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-19-2003 10:58 AM
06-19-2003 10:58 AM
Re: Shell Programming
Thank you for shaking my head. I used your approach (trying to avoid the typing) but using the -o option and forgot to remove the exclamation (!). You are totally correct....Thank you for the correction.
Regards,
Dario
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-25-2003 04:27 AM
06-25-2003 04:27 AM
Re: Shell Programming
No - my example is correct. He's asking if pie does not equal apple OR pumpkin - hence the || operator. Whereas if he was checking to see if pie does not equal applor AND pumpkin then i'd use the &&
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-25-2003 04:39 AM
06-25-2003 04:39 AM
Re: Shell Programming
use the shell's built in test operator.
the next if statement calls an external test operator;
if [ $PIE = xxx ]; then blabla
fi
for faster processing use the internal test operator
if [[ "$PIE" != "apple" -a "$PIE" != "cherry" -a "$PIE" != "pumpkin" ]]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-25-2003 05:00 AM
06-25-2003 05:00 AM
Re: Shell Programming
#!/bin/sh
PIE=$1
if echo $PIE | egrep -i -q -e "^apple$|^cherry$|^pumpkin$"
then
echo "$PIE pie; umm-umm-good"
else
echo "I don't know about $PIE pie"
fi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-25-2003 05:08 AM
06-25-2003 05:08 AM
Re: Shell Programming
This variant uses 'select' : you have to choose a number instead of a fruit. See 'select' in man sh-posix.
Regards.
#!/usr/bin/sh
export PS3="Enter Fruit "
select VAR in apple cherry pumpkin
do
[ -z "$VAR" ] &&
{
echo "$REPLY is note a correct choice, exiting ..."
break
}
echo "You've chosen a $VAR"
done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-25-2003 06:19 AM
06-25-2003 06:19 AM
Re: Shell Programming
echo $PIE|egrep '^(apple|cherry|pumpkin)' 2>&1 > /dev/null
if test "$?" ne "0"
then
echo "ICKYPOO!!! I don't like $PIE pie!"
else
echo "Yummmm: a $PIE pie!"
fi
Chris<--regexps are your friend......
(if sometimes a very confusing friend)