Operating System - HP-UX
1824994 Members
2120 Online
109678 Solutions
New Discussion юеВ

Re: if.. then..else statement

 
SOLVED
Go to solution
Pando
Regular Advisor

if.. then..else statement

Dear Gurus,
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.

10 REPLIES 10
Joseph Loo
Honored Contributor
Solution

Re: if.. then..else statement

hi,

if [ var$1 -eq $var2 ] && [ $var3 = $var4 ]
then ....
else ....
fi

regards.
what you do not see does not mean you should not believe
Biswajit Tripathy
Honored Contributor

Re: if.. then..else statement

If $var1/2/3/4 are integer numbers, you should use
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
:-)
Muthukumar_5
Honored Contributor

Re: if.. then..else statement

You can do this as,

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
Easy to suggest when don't know about the problem!
Trond Haugen
Honored Contributor

Re: if.. then..else statement

This should do the trick:
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
Regards,
Trond Haugen
LinkedIn
Jdamian
Respected Contributor

Re: if.. then..else statement

An important detail about replies above:

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.
Nguyen Anh Tien
Honored Contributor

Re: if.. then..else statement

Hmm do not use and operator, shell use (&&) as and operation.
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
HP is simple
Rodney Hills
Honored Contributor

Re: if.. then..else statement

if [ $var1 -eq $var2 -a $var3 = $var4 ]

-eq for numeric compare
= for string compare
-a is "and" operator

Do a "man test" for more info

HTH

-- Rod Hills
There be dragons...
Rick Garland
Honored Contributor

Re: if.. then..else statement

Use the double && for the logical AND.

The -a will work as well.

Nguyen Anh Tien
Honored Contributor

Re: if.. then..else statement

Fernando
do not forget to assign point to answer
HP is simple
Rory R Hammond
Trusted Contributor

Re: if.. then..else statement

OK here it is to an extreme


#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
There are a 100 ways to do things and 97 of them are right