Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Simple Bash script error  "integer expression expected" ?

Posted on 2009-12-27
8
Medium Priority
?
1,328 Views
Last Modified: 2013-12-26
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

0
Comment
Question by:smksa
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 85

Accepted Solution

by:
ozo earned 1400 total points
ID: 26129527
you can remove the non integer part with
OVERLOAD=`cat /proc/loadavg|awk '{ print $1 }'| sed 's/^[ \t]*//;s/[ \t]*$//;s/\..*//'`
0
 
LVL 2

Author Comment

by:smksa
ID: 26129544
the command cat /proc/loadavg|awk '{ print $1 }'| sed 's/^[ \t]*//;s/[ \t]*$//;s/\..*//' produce out put only "0" and not decimals.
0
 
LVL 85

Expert Comment

by:ozo
ID: 26129562
the decimal part is what causes the error
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 2

Author Comment

by:smksa
ID: 26129695
Thanks ozo ...

do you know any workaround for this ?
0
 
LVL 48

Expert Comment

by:Tintin
ID: 26129848
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

0
 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 200 total points
ID: 26129850
Slight correct to the above script

/proc/cpuload

should be

/proc/loadavg
0
 
LVL 7

Assisted Solution

by:hemmi
hemmi earned 400 total points
ID: 26129907
bash cannot do calculations on floating point numbers.
Convert the floating first to an integer.
loadavg will have two decimal digits behind the decimal point. Therefore multiply it by 100 and do comparisons relative to 100. You can use "bc" for the floating point multiplication.

Overload100=$(echo "z=${OVERLOAD} * 100; z/1"|bc)

That would lead to OVERLOAD100 being 61 in your above example. Compare that to 100 instead of 1.

0
 
LVL 85

Expert Comment

by:ozo
ID: 26129925
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
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Six Sigma Control Plans
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question