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
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
assign a variable to it, just like you did Code: variable=$(uptime | awk -v max="2" '$NF>max{ print $NF}')
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
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) }'
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
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.
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