Rusty Rusty - 2 months ago 6
Linux Question

Why this python script is giving wrong output,using awk and comapring value in if block?

I want to get desktop notification whenever load is more than five,for that I have written this python script but it is giving opposite to expected

#!/usr/bin/python
import commands

a=commands.getoutput("cat /proc/loadavg | awk '{print $1}'")
float (a)
print a
if (a > 5.00):
commands.getoutput('notify-send "Hello world!" ')
else:
print "load looks fine!!"


Can someone rectify this ?

Answer

You need to assign the typecast float value back to a

#!/usr/bin/python
import commands

a = commands.getoutput("cat /proc/loadavg | awk '{print $1}'")
a = float(a) # assign back to a
print a
if a > 5.00:
    commands.getoutput('notify-send "Hello world!" ')
else:
    print "load looks fine!!"