Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

comparing floating point with borne shell

SOLVED
Go to 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
1 ACCEPTED SOLUTION
Acclaimed Contributor Acclaimed Contributor

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.
4 REPLIES
Acclaimed Contributor Acclaimed Contributor

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 Honored Contributor

Re: comparing floating point with borne shell

echo '88.11' | sed -e 's/\.[0-9]*$//'
Honored Contributor Honored Contributor

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

Re: comparing floating point with borne shell

Thanks, all the solutions that came in work.
hilo