LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-17-2013, 11:06 AM   #1
Lucard
LQ Newbie
 
Registered: Feb 2010
Location: Lancaster Ohio
Distribution: Ubuntu
Posts: 19

Rep: Reputation: 0
Question Error in Shell script if then - line 21: [: 0.38: integer expression expected


#!/bin/bash
# Check cpu usage and if above threshold send message to screen.
# Define Threshold
THRESHOLD=150 ## in percentage

# Obtain the cpu usage
cpuLoad=$(uptime | sed -e "s/.*load average: \(.*\...\), \(.*\...\), \(.*\...\)/\1/" -e "s/ //g")
echo $cpuLoad

if [ "$cpuLoad" -gt "$THRESHOLD" ]; then
echo "CPU load over threshold!"
else
echo "CPU load ok"
fi
My output gives me cpuLoad.sh: line 19: [: 0.23: integer expression expected
 
Old 04-17-2013, 11:09 AM   #2
Lucard
LQ Newbie
 
Registered: Feb 2010
Location: Lancaster Ohio
Distribution: Ubuntu
Posts: 19

Original Poster
Rep: Reputation: 0
I have change line 19
if (( cpuLoad > 150 )); then
This didnot help
 
Old 04-17-2013, 11:11 AM   #3
MPH426
LQ Newbie
 
Registered: Feb 2013
Posts: 17

Rep: Reputation: Disabled
The answer is all in the output. You have a decimal number and it expects an integer. You'll need to multiply the decimal number by 100 to get a integer expression of the percentage.

cpuLoad=`echo "scale=0; "$cpuLoad" * 100" | bc -l | sed -e 's/\..*$//'`

Last edited by MPH426; 04-17-2013 at 11:24 AM. Reason: Code added
 
Old 04-17-2013, 11:42 AM   #4
Lucard
LQ Newbie
 
Registered: Feb 2010
Location: Lancaster Ohio
Distribution: Ubuntu
Posts: 19

Original Poster
Rep: Reputation: 0
Thank you for your help I have it working now.[COLOR="Silver"]

---------- Post added 04-17-13 at 12:43 PM ----------

#!/bin/bash
# 04/16/2013
# Lucard and a lot of help from MPH426 on here
# Check cpu usage and if above threshold send to screen.

# Define Threshold
threshold=150 ## in percentage

# wait time(in seconds) after sending mail
waitFor=1


while :
do
sleep $waitFor
# Obtain the cpu usage as a whole number
cpuLoad=$(uptime | sed -e "s/.*load average: \(.*\...\), \(.*\...\), \(.*\...\)/\1/" -e "s/ //g")
cpuLoad=`echo "scale=0; "$cpuLoad" * 100" | bc -l | sed -e 's/\..*$//'`



if (($cpuLoad > $threshold)); then
echo $cpuLoad
echo "CPU load over threshold!"
fi
done

Last edited by Lucard; 04-17-2013 at 11:53 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script error : integer expression expected linuxandtsm Linux - Newbie 7 01-07-2014 06:01 AM
[: : integer expression expected if else condition Iyyappan Linux - Server 4 11-25-2012 02:09 AM
[SOLVED] if condition error: integer expression expected Kashif_Bash Programming 6 04-18-2012 05:37 AM
Binary Operator Expected error in Shell Script mangatmodi Programming 9 10-25-2009 01:36 AM
Error upgrading kernel, /sbin/mkinitrdi integer expression expected amp2000 Linux - General 2 12-05-2003 05:38 AM


All times are GMT -5. The time now is 08:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration