- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: if.. then..else statement
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
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
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
тАО03-29-2005 03:05 PM
тАО03-29-2005 03:05 PM
Am creating a script for testing two variable. The 2 variable that needs to be satisified before executing the command. I would like to ask what should the syntax be?
Is it...
If [ var$1 -eq $var2 ] and [ $var3 = $var4 ]
then ....
else ....
fi
Maximum points for all correct replies.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-29-2005 03:17 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-29-2005 03:37 PM
тАО03-29-2005 03:37 PM
Re: if.. then..else statement
following:
if [ $var1 -eq $var2 ] && [ $var3 -eq $var4 ]
then
..
else
..
fi
You could use = instead of -eq in the if statement
and it would work just as good even if var1/2/3/4 are
integers or strings.
- Biswajit
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-29-2005 04:47 PM
тАО03-29-2005 04:47 PM
Re: if.. then..else statement
if [[ $var1 -eq $var2 && $var3 -eq $var4 ]]
then
echo "Correct"
else
echo "wrong"
fi
Example:
#!/bin/bash
var1=1
var2=1
var3=3
var4=3
if [[ $var1 -eq $var2 && $var3 -eq $var4 ]]
then
echo "Correct"
else
echo "wrong"
fi
Output:
Correct
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-29-2005 05:02 PM
тАО03-29-2005 05:02 PM
Re: if.. then..else statement
if [ "var$1" -eq "one" ] && [ "$var2" -eq "two" ]
then
....
else
....
fi
I like to quote the variables in case they are not set. That way you at least have an emty string.
Regards,
Trond
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-29-2005 09:50 PM
тАО03-29-2005 09:50 PM
Re: if.. then..else statement
Beware of '-eq' and '=' operators. They are slightly different: '-eq' is a integer comparison but '=' is a string comparison.
Examples:
[[ 0 -eq 0000 ]]; echo $? # shows 0 (true)
[[ 0 = 0000 ]]; echo $? # shows 1 (false)
Of course, don't forget to surround each $var1 with double quotes to prevent null chars.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-29-2005 09:59 PM
тАО03-29-2005 09:59 PM
Re: if.. then..else statement
like this:
if [ var$1 -eq $var2 ] && [ $var3 = $var4 ]
then
your_commands
else
your_commands
fi
That all
You can refer docs at
http://docs.hp.com/en/B2355-90046/index.html
HTH
tienna
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-30-2005 02:41 AM
тАО03-30-2005 02:41 AM
Re: if.. then..else statement
-eq for numeric compare
= for string compare
-a is "and" operator
Do a "man test" for more info
HTH
-- Rod Hills
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-30-2005 02:43 AM
тАО03-30-2005 02:43 AM
Re: if.. then..else statement
The -a will work as well.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-30-2005 09:48 PM
тАО03-30-2005 09:48 PM
Re: if.. then..else statement
do not forget to assign point to answer
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-31-2005 03:35 AM
тАО03-31-2005 03:35 AM
Re: if.. then..else statement
#doit XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
var1=$1
var2=$2
var3=$3
var4=$4
if
[ "${var1}" = "${var2}" -a "${var3}" = "${var4}" ]
then
echo "${var1} equals ${var2} and ${var3} equals ${var4}"
elif
[ "${var1}" != "${var2}" -a "${var3}" = "${var4}" ]
then
echo "${var1} does not equal ${var2} and ${var3} equal ${var4}"
elif
[ "${var1}" = "${var2}" -a "${var3}" != "${var4}" ]
then
echo "${var1} equal ${var2} and ${var3} does not equal ${var4}"
else
echo "${var1} does not equal ${var2} and ${var3} does not equal ${var4}"
fi
##########################
#doit one one two two
one equals one and two equals two
#doit 1 2 3 4
1 does not equal 2 and 3 does not equal 4
#doit 1 1 3 4
1 equal 1 and 3 does not equal 4
Hope it is readable.
Rory