Link to home
Start Free TrialLog in
Avatar of Shamsul Kamal
Shamsul Kamal

asked on

Simple Bash script error "integer expression expected" ?

Hi,

I would like to request an assistant.

I have deloped a simple bash script as attached.

It seems upon running it, i received the following error :

root@svr17 [/]# ./processcpu.sh
./processcpu.sh: line 6: [: 0.61: integer expression expected

Appreciates anybody assistant.

Thank you.

#!/bin/bash

host=`hostname`

OVERLOAD=`cat /proc/loadavg|awk '{ print $1 }'|sed 's/^[ \t]*//;s/[ \t]*$//'`
if [ "$OVERLOAD" -gt "1" ];
then
echo 'OVERLOAD ! - '$host' is overload and will down within a few minutes..

Monitor the server immediately..

PLEASE CHECK NOW !!

Date :'`date` | mail -s "SERVER OVERLOAD: '$host' having CPU LOAD '$OVERLOAD'" email@email.com
exit 1
fi

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Shamsul Kamal
Shamsul Kamal

ASKER

the command cat /proc/loadavg|awk '{ print $1 }'| sed 's/^[ \t]*//;s/[ \t]*$//;s/\..*//' produce out put only "0" and not decimals.
the decimal part is what causes the error
Thanks ozo ...

do you know any workaround for this ?
I assume checking for a load average > 1 was just for testing, otherwise you'll be getting a lot of emails.


#!/bin/bash
LOAD=$( awk '$1 > 1 {print $1}' /proc/cpuload)
HOST=$(hostname)
 
[ -z "$LOAD" ] && exit
 
cat <<EOF
OVERLOAD ! - '$HOST' is overload and will down within a few minutes..
  
Monitor the server immediately..
 
PLEASE CHECK NOW !!
EOF
 
date | mail -s "SERVER OVERLOAD: '$HOST' having CPU LOAD '$LOAD'" email@email.com
exit 1

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Unless it is important that the decision point is between 1.00 and 1.01 rather than between 0.99 and 1.00
you could just strip the decimal portion and compare -ge 1 instead of -gt 1