Re: comparing floating point with borne shell (238 Views)
Posts: 46
Registered: ‎08-28-2000
Message 1 of 5 (238 Views)

# comparing floating point with borne shell

I have a script that awk the cpu percentage from a captured top screen.

pcent=`egrep "noc" |awk '{print \$10}'1

Then I compare pcent with 90 because if the process is greater than 90 I want to kill the process.
hival=90
if [ \$pcent -gt \$hival ]
then
(here is where I kill the process)

I guess when pcent have a value of 88.11 it does not test correctly. Any way to convert 88.11 to a whole value instead of a decimal point value.
hilo
Acclaimed Contributor
Posts: 26,108
Registered: ‎03-06-2006
Message 2 of 5 (238 Views)

# Re: comparing floating point with borne shell

>comparing floating point with borne shell

These are not the droids you want. :-)
There is no such thing as borne shell, only ksh and posix shell.

>Any way to convert 88.11 to a whole value instead of a decimal point value.

Just use awk's printf with integer format:
awk '{printf "%d\n", \$10}'

Adding +.5 if you want to round up.
Honored Contributor
Posts: 9,173
Registered: ‎02-23-2005
Message 3 of 5 (238 Views)

# Re: comparing floating point with borne shell

echo '88.11' | sed -e 's/\.[0-9]*\$//'
Honored Contributor
Posts: 1,575
Registered: ‎02-10-2003
Message 4 of 5 (238 Views)

# Re: comparing floating point with borne shell

Hi,

since you already have awk in use, let the work be done here. This makes no sense, however:
>>
pcent=`egrep "noc" |awk '{print \$10}'1
<<
So lets assume you get it anyway (substitute 'print 77.95' by your command):

high=90
print 77.95 | awk -v h=high '{if (\$1 >h) exit 1;exit 0}'
if [ \$? -gt 0 ]
then
print CPU-usage more then \$high
fi

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Highlighted