LinuxQuestions.org
Review your favorite Linux distribution.
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
 
LinkBack Search this Thread
Old 11-02-2006, 07:18 PM   #1
bullfrog1870
LQ Newbie
 
Registered: Nov 2006
Posts: 14

Rep: Reputation: 0
shell (bash) scripting - strings vs. integers


Using FC5 with /bin/bash

I'm trying to figure out how to get the CPU load average as an integer so my script can take further action if greater than x%.

I wanted to use "uptime | cut -d, -f4". However, I can't use the -gt operator since it requires an integer and the above mentioned command returns a string.

Can I convert string to integer somehow?

Thanks...
 
Old 11-02-2006, 07:44 PM   #2
//////
Member
 
Registered: Nov 2005
Location: Land of Linux :: Finland
Distribution: Quad boot :: Windows vista 64-bit | Vector Linux | Slackware 13.0 64-Bit | Debian 6.0 64-bit
Posts: 138

Rep: Reputation: 17
Code:
uptime |awk '{print $10}' |sed -e 's/,//g'
Output:
2.30

Edit:

I used this one:

Code:
TEST=$(uptime |awk '{print $10}' |sed -e 's/,//g')

if [[ "$TEST" > "0.80" ]]; then
echo "load over 0.80"
else
echo "load under 0.80"
fi

Last edited by //////; 11-02-2006 at 08:35 PM.
 
Old 11-02-2006, 08:00 PM   #3
chort
Senior Member
 
Registered: Jul 2003
Location: Silicon Valley, USA
Distribution: OpenBSD 4.6, OS X 10.6.2, CentOS 4 & 5
Posts: 3,660

Rep: Reputation: 69
My stab at it (there must be a more elegant way to do this):

Code:
#!/usr/local/bin/bash

foo=`uptime | cut -d ',' -f4 | tr -d '.' | tr -d [:blank:] | sed -e s/^0+//`

if [ $((foo)) -gt 100 ]
then
        echo "Load over 1.00"
else
        echo "Load under 1.01"
fi
BTW I suppose this could use an explanation. Suppose you got 0.06 for the load average. Here is what it would look like after each step above (cut, tr, tr, sed):
Code:
 0.06
 006
006
6
The reason you need to strip the leading 0s is because bash will assume it's octal if it starts with a 0. The $(()) around foo forces arithmatic comparison.

Last edited by chort; 11-02-2006 at 08:12 PM.
 
Old 11-02-2006, 08:51 PM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 26,519
Blog Entries: 51

Rep: Reputation: 2598Reputation: 2598Reputation: 2598Reputation: 2598Reputation: 2598Reputation: 2598Reputation: 2598Reputation: 2598Reputation: 2598Reputation: 2598Reputation: 2598
How about (Bash):
Code:
chkavg() { lim=30; avg=(`uptime`); avg=${avg[8]//,/}; return $(bc -l <<< "$avg>$lim"); }
Uptime gives a space separated string so we can use an array w/o needing to muck with the IFS, pick the nineth element and strip off the comma, do a here bc thingie (couldn't make $(()) work) and return the result since that's all that is necessary for further ops.
 
  


Reply

Tags
bash, scripting


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash shell scripting using if else nautz Linux - General 10 11-07-2006 09:47 AM
Bash scripting: column-ize file of varying length strings Quantum0726 Programming 4 08-13-2005 06:19 PM
converting integers to strings 1337 Twinkie Programming 12 08-06-2004 04:57 PM
Shell scripting: exclamation marks in strings Dark_Helmet Programming 9 06-16-2003 06:32 PM
Bash Shell Scripting Help Tangerine Programming 6 05-06-2003 02:10 PM


All times are GMT -5. The time now is 02:36 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration