Operating System - HP-UX
1829576 Members
2787 Online
109992 Solutions
New Discussion

Can not seem to get this IF statement to work

 
SOLVED
Go to solution
Scott Frye_1
Super Advisor

Can not seem to get this IF statement to work

I have three variables
$WCPU = 0.48
$HIGHWATER 0.10
$COUNTER 1
I want to do something like this
If $WCPU is greater than or equal to $HIGHWATER AND $COUNTER is equal to 0 then do something.

I've tried if [$WCPU -ge $HIGHWATER -a $COUNTER -eq 0] but it doesn't work. I get
./toptest.sh[23]: [0.48: not found.
12 REPLIES 12
Scot Bean
Honored Contributor

Re: Can not seem to get this IF statement to work

When using -ge, you need double brackets, also whitespace.

Try [[ $WCPU -ge $HIGHWATER -a $COUNTER -eq 0 ]]
Jerry Jordak
Advisor

Re: Can not seem to get this IF statement to work

Leave a space between the brackets and the characters inside, like this:

if [ $WCPU -ge $HIGHWATER -a $COUNTER -eq 0 ]

From your error message, it appears the shell is trying to evaluate [$WCPU as a command.

Also, AFAIK, ksh can only handle integers in variables for purposes of determining greater than and less than. It will evaluate 0.48 and 0.10 as 0 in the if statement. You may want to change your numbering to use integers instead.

-JWJ
Scott Frye_1
Super Advisor

Re: Can not seem to get this IF statement to work

Jerry,

What are you suggesting I do with my variables?
Pete Randall
Outstanding Contributor

Re: Can not seem to get this IF statement to work

Scott,

Isn't this similar to the problem you had a while back:

http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=737698

To quote Clay, "The problem is that the shell only does integer arithmetic. Use awk or bc."


Pete

Pete
Jerry Jordak
Advisor

Re: Can not seem to get this IF statement to work

In cases like this, where I know I might have numbers with values between 0 and 1, I've multiplied the values of the variables by 100, so that the if statements will work properly.

For example, if you define the variables as so:

WCPU=48
HIGHWATER=10

then it will work. If you're getting the $WCPU value from another command or a script, you may need to convert it, by removing the decimal point.
Scott Frye_1
Super Advisor

Re: Can not seem to get this IF statement to work

Pete,

I believe that is in the same ball park. I had forgetten about that thread. I quickly manned bc and I found it does not currently support && or ||. Also, I'm not clear on it usage in my if statement.

If you have done anything like this before, I would enjoy seeing some examples.

Thanks again for refreshing my memory.

Scott
Pete Randall
Outstanding Contributor

Re: Can not seem to get this IF statement to work

Scott,

I'm not all that great on shell scripting myself - I only stumbled over that thread while I was searching the ITRC for a solution to your problem.


Pete

Pete
Michael Schulte zur Sur
Honored Contributor

Re: Can not seem to get this IF statement to work

Scott,

if you format wcpu and highwater the same way you can compare them as strings.
WCPU=`printf %06.2f 0.48`
HIGHWATER==`printf %06.2f 0.10`
if [ "$WCPU" > "$HIGHWATER" ]..

greetings,

Michael
Chris Vail
Honored Contributor

Re: Can not seem to get this IF statement to work

As others have pointed out, ksh only does integer math. You can fix your numbers with:
NCPU=(( "$WCPU" * "100" ))
NHIGHWATER=(( "$HIGHWATER" * "100" ))
if test "$NHIGHWATER" -eq "$NCPU" -a "$COUNTER1" -eq "0"
then
do something
fi

The double parenthesis is ksh's version of the "let" statement.
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Can not seem to get this IF statement to work

NCPU=(( "$WCPU" * "100" ))
is going to evaluate as 0 if ${WCPU} is 0.48)

Here's a method to leverage bc for the floating-point evaluation. It will return 1 if ${WCPU} >= ${HIGHWATER} and 0 otherwise.

#!/usr/bin/sh

WCPU=0.48
HIGHWATER=0.10
COUNTER=1

typeset -i A=$(echo "a = 0; if (${WCPU} >= ${HIGHWATER}) a = 1;a" | bc)
# now we can do a compound if
if [[ ${A} -ne 0 && ${COUNTER} -eq 0 ]]
then
echo "Do your thing"
fi


If it ain't broke, I can fix that.
Michael Schulte zur Sur
Honored Contributor

Re: Can not seem to get this IF statement to work

Clay,

your routine does not work if for example
WCPU=10.10
HIGHWATER=4.1

It seems that not every shell supports string1 > string2 comparisons.

Inspired of Chris another try:
#!/usr/bin/sh -x
WCPU=`eval echo 0.08 | awk '{printf("%06.2f",$1*100)}'`
HIGHWATER=`eval echo 0.18 | awk '{printf("%06.2f",$1*100)}'`
if test ${WCPU} -gt ${HIGHWATER}
then
echo do your thing
fi

greetings,

Michael
Scott Frye_1
Super Advisor

Re: Can not seem to get this IF statement to work

Thank you to all who posted to this thread. Although I'm not quite clear what it is doing yet (I love learning this way), I'm going to take Clay's approach.

Thanks again to all.