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
Welcome to
LinuxQuestions.org , a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free.
Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please
contact us . If you need to reset your password,
click here .
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
11-01-2003, 11:23 AM
#1
LQ Newbie
Registered: Oct 2003
Location: Indy
Posts: 14
Rep:
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,
11-01-2003, 11:48 AM
#2
Member
Registered: Oct 2003
Location: Rochester, New York (USA)
Distribution: Debian
Posts: 119
Rep:
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
11-01-2003, 12:24 PM
#3
LQ Newbie
Registered: Oct 2003
Location: Indy
Posts: 14
Original Poster
Rep:
This should capture the variable.
echo "This is the 5min load average:"
FIVE_MIN_LOADAVE=`uptime | cut -d ',' -f4`
echo "$FIVE_MIN_LOADAVE"
11-01-2003, 12:39 PM
#4
Member
Registered: Oct 2003
Location: Rochester, New York (USA)
Distribution: Debian
Posts: 119
Rep:
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
11-01-2003, 01:03 PM
#5
LQ Newbie
Registered: Oct 2003
Location: Indy
Posts: 14
Original Poster
Rep:
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
11-01-2003, 01:07 PM
#6
Member
Registered: Oct 2003
Location: Rochester, New York (USA)
Distribution: Debian
Posts: 119
Rep:
What does the line :
echo "$FIVE_MIN_AVE" print out ?
11-01-2003, 01:08 PM
#7
LQ Newbie
Registered: Oct 2003
Location: Indy
Posts: 14
Original Poster
Rep:
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
11-01-2003, 01:10 PM
#8
LQ Newbie
Registered: Oct 2003
Location: Indy
Posts: 14
Original Poster
Rep:
What does the line :
echo "$FIVE_MIN_AVE" print out ?
I does give me the correct load average
.14
11-01-2003, 01:14 PM
#9
LQ Newbie
Registered: Oct 2003
Location: Indy
Posts: 14
Original Poster
Rep:
It dosen't work after all!
11-01-2003, 01:19 PM
#10
Member
Registered: Oct 2003
Location: Rochester, New York (USA)
Distribution: Debian
Posts: 119
Rep:
If I were you I'd investigate doing this with something that understands FP arithmetic, like Perl.
11-01-2003, 01:42 PM
#11
LQ Newbie
Registered: Oct 2003
Location: Indy
Posts: 14
Original Poster
Rep:
My next questionis, how do you compare two strings? One of the strings being a range from 6-10.
11-01-2003, 02:01 PM
#12
Member
Registered: Oct 2003
Location: Rochester, New York (USA)
Distribution: Debian
Posts: 119
Rep:
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
Thread Tools
Search this Thread
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT -5. The time now is 10:32 AM .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know .
Latest Threads
LQ News