Operating System - HP-UX
1748227 Members
4341 Online
108759 Solutions
New Discussion юеВ

Re: 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.