Operating System - HP-UX
1825667 Members
4554 Online
109686 Solutions
New Discussion

Unix easy question: How do I do one IF evaluating tow conditions

 
SOLVED
Go to solution
GerGon
Regular Advisor

Unix easy question: How do I do one IF evaluating tow conditions

Hi..
I want to evalute in ksh two expressions into one IF statement:

** The problem there is in: elif below, How do I to compare $b1 -gt 20 and $dia -eq 3

Thanks...

b1=20
dia=3
#dia=`date "+ %u"`
echo $b1 > algo
echo $dia >> algo
if [ $b1 -gt 24 ]
then
elm -s "Important" bb@cr.com < $HOME/bin/Mensaje
cat $HOME/bin/Mensaje | mailx -s Respaldo ggonzalez@cr.freshdelmonte.com
#cp $HOME/bin/cero $HOME/bkdb/a1
elif ( $b -gt 19; $dia -eq 3 )
then
cat $HOME/bin/Mensaje | mailx -s Hi xx@yy
fi
15 REPLIES 15
Rodney Hills
Honored Contributor

Re: Unix easy question: How do I do one IF evaluating tow conditions

Use the "-a" connector (for and, -o for or).

if [[ $b1 -gt 19 -a $dia -eq 3 ]] ; then
echo "it is true for both"
fi

HTH

-- Rod Hills
There be dragons...
A. Clay Stephenson
Acclaimed Contributor

Re: Unix easy question: How do I do one IF evaluating tow conditions

Rodney's reply is not quite correct as the syntax changes if using the shell's (POSIX or Korn) internal test '[[ ]]' as opposed to the external test command '[ ]'.

(iNTERNAL TEST SYNTAX - faster, cheaper)
if [[ $b1 -gt 19 && $dia -eq 3 ]]
then
..
..
fi

(EXTERNAL TEST COMMAND SYNTAX)
if [ $b1 -gt 19 && $dia -eq 3 ]
then
..
..
fi


Now to be nit-picky you should really enclose any variables in {}'s
so
if [[ ${b1} -gt 19 && ${dia} -eq 3 ]]
If it ain't broke, I can fix that.
Mark Grant
Honored Contributor

Re: Unix easy question: How do I do one IF evaluating tow conditions

Rodney's suggestion is the sensible and quickest. However a useful construct in other cases like this is the &&. This says if the left hand condition is true then do the right hand expression. You don't even need the "if" then. Due to the formatting on ITRC forums at the moment please consider
to be a newline. This would give you [ $b -gt 20 ] && [ $dia -eq 3 ] && {
mailx -s Hi xx@yy
}
The opposite of && is ||
Never preceed any demonstration with anything more predictive than "watch this"
Adam J Markiewicz
Trusted Contributor

Re: Unix easy question: How do I do one IF evaluating tow conditions

I'm not the best in shell scrpts, but whats wrong with most obious?

elif ( $b -gt 19 && $dia -eq 3 )

Good luck
Adam
I do everything perfectly, except from my mistakes
Adam J Markiewicz
Trusted Contributor

Re: Unix easy question: How do I do one IF evaluating tow conditions

Sorry, ignore that.
I do everything perfectly, except from my mistakes
Mark Grant
Honored Contributor

Re: Unix easy question: How do I do one IF evaluating tow conditions

Adam, because you would also need to put a test in there! The "(" just executes the arguments in a sub-shell.
Never preceed any demonstration with anything more predictive than "watch this"
GK_5
Regular Advisor

Re: Unix easy question: How do I do one IF evaluating tow conditions

elif [ $b -gt 19 -a $diag -eq 3 ]

Should work.
IT is great!
GerGon
Regular Advisor

Re: Unix easy question: How do I do one IF evaluating tow conditions

Sorry but it doesn't work, do anything.

This is not work: elif [ $b -gt 19 -a $diag -eq 3 ]

Bellow don't show any error message but don't do anything...

elif [[ ${b} -gt 19 && ${dia} -eq 3 ]]
then
cat $HOME/bin/Mensaje | mailx -s Respaldo ggon@cr.com
echo "hola"
echo "hola"
echo "hola"
fi
~
~
~
~
~
"CtrlDayToBk" 18 lines, 493 characters
[oracle8i@bandeco]/home/oracle8i/bin> CtrlDayToBk
[oracle8i@bandeco]/home/oracle8i/bin>
GerGon
Regular Advisor

Re: Unix easy question: How do I do one IF evaluating tow conditions

I tried to trace it putting the echoes.. but don't show nothing.

Will it be the "then" below the elif?
Mark Grant
Honored Contributor

Re: Unix easy question: How do I do one IF evaluating tow conditions

I just tested this and scratched my head for a while thinking "wow, it doesn't work" until I realised that my original "if" condition was evaluating to true and therefore the "elif" wasn't being executed. That's a "Doh!" moment. Are you sure your original "if" is evaluating as false? or at least produces a non zero exit status?
Never preceed any demonstration with anything more predictive than "watch this"
GerGon
Regular Advisor

Re: Unix easy question: How do I do one IF evaluating tow conditions

Mark, as you can see bellow, the variables are static, then the program must be entry in the "elif" condition.

Any echo was showed

What's wrong here!!!!!???!!!


[oracle8i@bandeco]/home/oracle8i/bin> cat CtrlDayToBk
#b1=`cat $HOME/bkdb/a1`
b1=20
dia=3
#dia=`date "+ %u"`
echo $b1 > algo
echo $dia >> algo
if [ $b1 -gt 24 ]
then
echo " Primer IF"
elm -s "Importante" esandoval@cr.com < $HOME/bin/Mensaje
cat $HOME/bin/Mensaje | mailx -s Respaldo ggo@cr.com
#cp $HOME/bin/cero $HOME/bkdb/a1
elif [[ ${b} -gt 19 && ${dia} -eq 3 ]]
then
cat $HOME/bin/Mensaje | mailx -s Respaldo gg@cr.com
echo "hola"
echo "hola"
echo "hola"
fi
[oracle8i@bandeco]/home/oracle8i/bin> cat algo
20
3
[oracle8i@bandeco]/home/oracle8i/bin>
Mark Grant
Honored Contributor
Solution

Re: Unix easy question: How do I do one IF evaluating tow conditions

I might be missing something in the tiny font I have at the moment but you appear to be testing $b where the rest of the script is using $b1


$b seems to be 0 to me.
Never preceed any demonstration with anything more predictive than "watch this"
john korterman
Honored Contributor

Re: Unix easy question: How do I do one IF evaluating tow conditions

Hi,
you set your variable as
b1

but in your elif you test for
b

regards,
John K.
it would be nice if you always got a second chance
Patrick Wallek
Honored Contributor

Re: Unix easy question: How do I do one IF evaluating tow conditions

You've got a typographical error in your elif statement. You've ${b} instead of ${b1} in the condition. Change that and your script should work.
GerGon
Regular Advisor

Re: Unix easy question: How do I do one IF evaluating tow conditions

Big excuses... You got it...!!!

I miss the $b1...

Thanks to all... a lot, NOW it works fine..!!