Re: comparing floating point with borne shell (225 Views)
Reply
Advisor
Clarence Rebolledo
Posts: 46
Registered: ‎08-28-2000
Message 1 of 5 (225 Views)
Accepted Solution

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
Dennis Handly
Posts: 25,199
Registered: ‎03-06-2006
Message 2 of 5 (225 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
Steven Schweda
Posts: 9,091
Registered: ‎02-23-2005
Message 3 of 5 (225 Views)

Re: comparing floating point with borne shell

echo '88.11' | sed -e 's/\.[0-9]*$//'
Honored Contributor
Peter Nikitka
Posts: 1,575
Registered: ‎02-10-2003
Message 4 of 5 (225 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"
Advisor
Clarence Rebolledo
Posts: 46
Registered: ‎08-28-2000
Message 5 of 5 (225 Views)

Re: comparing floating point with borne shell

Thanks, all the solutions that came in work.
hilo
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.