- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Newbie script question...
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-18-2006 06:37 PM
11-18-2006 06:37 PM
If var1="Dead" And var2="Live" then
'Do something...
ElseIf var1="Live" And var2="Dead" then
'Do something...
End If
How can I do that in a POSIX script?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-18-2006 07:33 PM
11-18-2006 07:33 PM
Solutionyou can try something like this:
$ cat decide.sh
#!/usr/bin/sh
if [[ ${1} = "Dead" && ${2} = "Alive" ]]
then
echo "${1} is Dead and ${2} is Alive"
elif [[ ${2} = "Dead" && ${1} = "Alive" ]]
then
echo "${1} is Alive and ${2} is Dead"
else
echo " neither nor..."
fi
And execute with two parameters, e.g.:
$ ./decide.sh Dead Alive
regards,
John K.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2006 04:56 AM
11-19-2006 04:56 AM
Re: Newbie script question...
another possibility would be a 'case' statement, which is easier to read, IMHO:
check="$var1"-"$var2" # use appropriate delimiter
Such use easily can check e.g. if 'at least one is alive':
case $check in
*Live*) echo Yeah ;;
esac
A complete check:
case $check in
Dead-Live) echo 1st;;
Live-Dead) echo 2nd ;;
Live-Live) echo both ;;
Dead-Dead) echo none ;;
esac
mfG Peter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2006 10:56 AM
11-19-2006 10:56 AM
Re: Newbie script question...
In the shell, '-a' represents 'and' and '-o' signifies 'or'. Parenthesis used for logical grouping need to be escaped. The '!' operator denotes negation. The '&&' and '||' are often used to create compound expressions.
Have a look at the manpages for 'test' and for 'sh-posix'.
http://www.docs.hp.com/en/B2355-60127/test.1.html
http://www.docs.hp.com/en/B2355-60127/sh-posix.1.html
Consider:
# cat testit
#!/usr/bin/sh
if [ \( "$1" = "dead" -o "$1" = "expired" \) -a "$2" != "alive" ]; then
echo "it's very dead!"
fi
[ \( "$1" = "dead" -o "$1" = "expired" \) -a "$2" != "alive" ] && echo "dead!";
exit 0
# ./testit expired unsure
it's very dead!
dead!
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2006 02:36 PM
11-19-2006 02:36 PM
Re: Newbie script question...
Thanks everyone!