LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 11-01-2003, 12:23 PM   #1
wbdune
LQ Newbie
 
Registered: Oct 2003
Location: Indy
Posts: 14

Rep: Reputation: 0
How do you convert a string to an integer?


#!/bin/bash


FIVE_MIN_LOADAVE= uptime | cut -d ',' -f4

if [ "$FIVE_MIN_LOADAVE" -ge "6" ]
then
echo "Everything is fine"
exit 2
fi

What I'm trying to do is check the load average on the system and send notifications of any problems.

I would like to work through the problem my self but I dont know how to convert a string to an integer. I get the fallowing error message:

[wade@localhost wade]$ bash loadtime
0.10
loadtime: line 6: [: : integer expression expected

Thanks,
 
Old 11-01-2003, 12:48 PM   #2
tgflynn
Member
 
Registered: Oct 2003
Location: Rochester, New York (USA)
Distribution: Debian
Posts: 119

Rep: Reputation: 15
You've got a few problems. First your line to capture the output of uptime doesn't work. To see this, try echoing your variable.

One reason it doesn't work is that you don't have the right syntax for capturing the output of a command into a shell variable. You need to enclose the command in `` (backquotes) or $( )

Secondly your cut operation doesn't return a number but the string :

load average: 0.36

(at least on my machine).

Finally the number you want to compare isn't an integer but a floating point and the shell doesn't support floating point arithmetic.

Tim
 
Old 11-01-2003, 01:24 PM   #3
wbdune
LQ Newbie
 
Registered: Oct 2003
Location: Indy
Posts: 14

Original Poster
Rep: Reputation: 0
This should capture the variable.

echo "This is the 5min load average:"

FIVE_MIN_LOADAVE=`uptime | cut -d ',' -f4`

echo "$FIVE_MIN_LOADAVE"
 
Old 11-01-2003, 01:39 PM   #4
tgflynn
Member
 
Registered: Oct 2003
Location: Rochester, New York (USA)
Distribution: Debian
Posts: 119

Rep: Reputation: 15
Re: This should capture the variable.

Quote:
Originally posted by wbdune
echo "This is the 5min load average:"

FIVE_MIN_LOADAVE=`uptime | cut -d ',' -f4`

echo "$FIVE_MIN_LOADAVE"
This will capture the 4th comma seperated field of the output of uptime. On my machine that's the string :

load average: 0.06

One way to get just the number is this :

FIVE_MIN_LOADAVE=`uptime | cut -d ',' -f4 |cut -f2 -d ':'`

Tim
 
Old 11-01-2003, 02:03 PM   #5
wbdune
LQ Newbie
 
Registered: Oct 2003
Location: Indy
Posts: 14

Original Poster
Rep: Reputation: 0
I cant get it to give the correct results

#!/bin/bash

echo "This is the 5min load average:"

FIVE_MIN_LOADAVE=`uptime | cut -d "," -f4`

echo "$FIVE_MIN_LOADAVE"

if [ " $FIVE_MIN_LOADAVE " != " 0.* , 7.* , 8.* , 9.* , 10 " ]
then
echo "Everyting is fine."
else
echo "Somthing is eating your resources!"
exit 0
fi
 
Old 11-01-2003, 02:07 PM   #6
tgflynn
Member
 
Registered: Oct 2003
Location: Rochester, New York (USA)
Distribution: Debian
Posts: 119

Rep: Reputation: 15
What does the line :

echo "$FIVE_MIN_AVE" print out ?
 
Old 11-01-2003, 02:08 PM   #7
wbdune
LQ Newbie
 
Registered: Oct 2003
Location: Indy
Posts: 14

Original Poster
Rep: Reputation: 0
I just answered my own question

#!/bin/bash

echo "This is the 5min load average:"

FIVE_MIN_LOADAVE=`uptime | cut -d "," -f4`

echo "$FIVE_MIN_LOADAVE"

if [ "$FIVE_MIN_LOADAVE" = "[6.*-10]" ]
then
echo "Everyting is fine."
else
echo "Somthing is eating your resources!"
exit 0
fi
 
Old 11-01-2003, 02:10 PM   #8
wbdune
LQ Newbie
 
Registered: Oct 2003
Location: Indy
Posts: 14

Original Poster
Rep: Reputation: 0
What does the line : echo "$FIVE_MIN_AVE" print out ?

I does give me the correct load average

.14
 
Old 11-01-2003, 02:14 PM   #9
wbdune
LQ Newbie
 
Registered: Oct 2003
Location: Indy
Posts: 14

Original Poster
Rep: Reputation: 0
It dosen't work after all!
 
Old 11-01-2003, 02:19 PM   #10
tgflynn
Member
 
Registered: Oct 2003
Location: Rochester, New York (USA)
Distribution: Debian
Posts: 119

Rep: Reputation: 15
If I were you I'd investigate doing this with something that understands FP arithmetic, like Perl.
 
Old 11-01-2003, 02:42 PM   #11
wbdune
LQ Newbie
 
Registered: Oct 2003
Location: Indy
Posts: 14

Original Poster
Rep: Reputation: 0
My next questionis, how do you compare two strings? One of the strings being a range from 6-10.
 
Old 11-01-2003, 03:01 PM   #12
tgflynn
Member
 
Registered: Oct 2003
Location: Rochester, New York (USA)
Distribution: Debian
Posts: 119

Rep: Reputation: 15
Here's a script that does about what you want (assuming you have perl installed) :

#!/bin/bash

FIVE_MIN_LOADAVE=$(uptime | cut -d ',' -f4 | tr -d 'a-zA-Z: ')
LIMIT=6.5

#echo "FIVE_MIN_LOADAVE=$FIVE_MIN_LOADAVE"

if perl -e "($FIVE_MIN_LOADAVE <= $LIMIT) or die"
then
echo "Everything is fine"
else
echo "Everything is not fine."
exit 2
fi
 
  


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
C++ string to integer conversion fatman Programming 6 06-18-2006 09:17 AM
C how to convert an integer to string? totti10 Programming 10 11-10-2005 06:49 AM
convert string to integer with python Kanaflloric Programming 2 05-27-2005 12:04 PM
Integer To a String in C Bean101 Programming 2 05-27-2004 05:46 AM
convert integer to string (atoi?) lea Programming 6 10-11-2002 06:16 PM


All times are GMT -5. The time now is 04:28 AM.

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