Go Back   HowtoForge Forums | HowtoForge - Linux Howtos and Tutorials > Linux Forums > Programming/Scripts

Do you like HowtoForge? Please consider supporting us by becoming a subscriber.
Reply
 
Thread Tools Display Modes
  #1  
Old 5th April 2008, 09:39
make-fun make-fun is offline
Member
 
Join Date: Jan 2008
Posts: 92
Thanks: 8
Thanked 8 Times in 7 Posts
Default AWK: convert string to numeric, how?

Hi folks,
what seemed very simple to me when I started working it out, has given me quite some headache by now
I want to to trigger some event, in case the server load of the last 15min breaches a defined MAX
Code:
isLoad15=$(uptime | awk '{ print $NF }')
maxLoad15=2.00

if [ "$isLoad15" gt "$maxLoad15" ]
then
But it turns out I simply can't convert the the
Code:
print $NF
into a numeric, as required for the "gt - condition".
One of the approaches I found so fare was to simply add a numeric value and thereby convert it
Code:
print "Numeric", "23" + 0 -> 23 is Numeric
but no matter what I tried i only got
Code:
gt: binary operator expected
Thanks for any solution
Reply With Quote
Sponsored Links
  #2  
Old 6th April 2008, 09:19
ghostdog74 ghostdog74 is offline
Junior Member
 
Join Date: Aug 2006
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Code:
uptime | awk -v max="2" '$NF>max{ 
 print "Threshold reached"
}'
Reply With Quote
  #3  
Old 6th April 2008, 10:08
make-fun make-fun is offline
Member
 
Join Date: Jan 2008
Posts: 92
Thanks: 8
Thanked 8 Times in 7 Posts
Default

Thanks ghostdog74,

this would mean I'd have to do it all in AWK, but I'd need the var in the shell script, as there are several existing scripts which only need the "isLoad15" value.

Cheers
Reply With Quote
  #4  
Old 6th April 2008, 17:59
ghostdog74 ghostdog74 is offline
Junior Member
 
Join Date: Aug 2006
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

assign a variable to it, just like you did
Code:
variable=$(uptime | awk -v max="2" '$NF>max{ print $NF}')
Reply With Quote
  #5  
Old 7th April 2008, 06:24
make-fun make-fun is offline
Member
 
Join Date: Jan 2008
Posts: 92
Thanks: 8
Thanked 8 Times in 7 Posts
Question

Just doesn't do it…
Code:
#!/bin/sh
uptime
uptime | awk -v max="0.05" '$NF>max{
 print "Threshold reached"
 }'

  isLoad15=$(uptime | awk -v max="0" '$NF>max{ print $NF}')
  echo "isLoad15--"$isLoad15"--"
  if [ "$isLoad15" gt "0" ]
  then
  echo "Threshold reached"
  fi
will result in:
Code:
sh -x test15.sh
+ uptime
 05:00:22 up 11 days, 20:48,  1 user,  load average: 0.00, 0.10, 0.18
+ uptime
+ awk -v max=0.05 '$NF>max{
 print "Threshold reached"
 }'
Threshold reached
++ uptime
++ awk -v max=0 '$NF>max{ print $NF}'
+ isLoad15=0.18
+ echo isLoad15--0.18--
isLoad15--0.18--
+ '[' 0.18 -gt 0 ']'
test15.sh: line 9: [: gt: binary operator expected
I don't get it… inside awk the var seems numeric, but once it's passed on it seems like a string

Last edited by make-fun; 7th April 2008 at 06:37.
Reply With Quote
  #6  
Old 7th April 2008, 06:59
ghostdog74 ghostdog74 is offline
Junior Member
 
Join Date: Aug 2006
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

bash don't do floats. and why do you want to do double checking of threshold when you have already done it in awk?

Code:
uptime | awk -v max="0.05" '$NF>max{
 print "Threshold reached"
 # do processing ...... eg if you want to move files
 # cmd = "mv \047 " filename "\047 destination"
 # system(cmd)
 }'
Reply With Quote
  #7  
Old 7th April 2008, 07:27
make-fun make-fun is offline
Member
 
Join Date: Jan 2008
Posts: 92
Thanks: 8
Thanked 8 Times in 7 Posts
Default

Yes I just got that too, when I gave it a value manually…
Code:
integer expression expected
reason for all this is that there are more scripts running, which all depend on that stupid 15minLoad value…
In the past there was one more script which was called in all of those and returned the 15minLoad value. Unfortunately that script got lost in the last crash and no one had any backup…

Edit:
The above was just a test script to compare the awk/bash results — I would not do double checking of course

Last edited by make-fun; 7th April 2008 at 07:32.
Reply With Quote
  #8  
Old 15th May 2008, 14:05
burschik burschik is offline
Junior Member
 
Join Date: May 2008
Posts: 24
Thanks: 0
Thanked 2 Times in 2 Posts
Default Don't use awk at all

read load1 load5 load15 procs procid < /proc/loadavg
Reply With Quote
  #9  
Old 8th June 2008, 20:12
zarrelli zarrelli is offline
Member
 
Join Date: Jul 2006
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Not really a good programming example, but I beg pardon...my headache is horrible today:

Code:
#!/bin/sh -x
uptime
uptime | awk -v max="0.05" '$NF>max{
 print "Threshold reached"
 }'

  isLoad15=$(uptime | awk -v max="0" '$NF>max{ print $NF}')
  echo "isLoad15--"$isLoad15"--"
  a=$(echo "scale=3; "$isLoad15">0" | bc)
  if [ $a -ne 0 ]
  then
  echo "Threshold reached"
  fi

Note that bc "inverts" the exit code, so we have to turn the matching.
Reply With Quote
  #10  
Old 9th June 2008, 09:48
burschik burschik is offline
Junior Member
 
Join Date: May 2008
Posts: 24
Thanks: 0
Thanked 2 Times in 2 Posts
 
Default I still say there is no need to use awk

Code:
#/bin/bash

threshold=0.05
read load1 load5 load15 procs procid < /proc/loadavg

echo "isLoad15--${load15}--"
if [ ${load15/./} -gt ${threshold/./} ]
    then echo "treshold exceeded"
fi
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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 On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Odd Vlogger Errors in error_log iyeat HOWTO-Related Questions 5 30th January 2008 16:36
So close to getting 3D on my ATI graphics card, please help me finish thirtythree HOWTO-Related Questions 6 27th September 2007 23:04
Fedora Core 5 Spamassassin (Scalix Install) Viper_iii Installation/Configuration 1 19th September 2007 15:59
reoback problems on debian 3.1 Ovidiu Server Operation 12 15th May 2007 23:25
Sudden Problem Started using cp: command "omitting directory" bpmee Server Operation 11 22nd November 2006 17:39


All times are GMT +2. The time now is 22:42.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.