PDA

View Full Version : AWK: convert string to numeric, how?


make-fun
5th April 2008, 09:39
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
isLoad15=$(uptime | awk '{ print $NF }')
maxLoad15=2.00

if [ "$isLoad15" gt "$maxLoad15" ]
then
…But it turns out I simply can't convert the the 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 print "Numeric", "23" + 0 -> 23 is Numeric
but no matter what I tried i only got gt: binary operator expected

Thanks for any solution

ghostdog74
6th April 2008, 09:19
uptime | awk -v max="2" '$NF>max{
print "Threshold reached"
}'

make-fun
6th April 2008, 10:08
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

ghostdog74
6th April 2008, 17:59
assign a variable to it, just like you did

variable=$(uptime | awk -v max="2" '$NF>max{ print $NF}')

make-fun
7th April 2008, 06:24
Just doesn't do it…

#!/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:

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 :confused:

ghostdog74
7th April 2008, 06:59
bash don't do floats. and why do you want to do double checking of threshold when you have already done it in awk?


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)
}'

make-fun
7th April 2008, 07:27
Yes I just got that too, when I gave it a value manually…
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

burschik
15th May 2008, 14:05
read load1 load5 load15 procs procid < /proc/loadavg

zarrelli
8th June 2008, 20:12
Not really a good programming example, but I beg pardon...my headache is horrible today:

#!/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.

burschik
9th June 2008, 09:48
#/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