Ankit Kushwaha Ankit Kushwaha -4 years ago 94
Linux Question

comparing two variables value but if condition does not satisfied

i am new to bash script. I am making a script to calculate cpu usage. So what I am doing is calculating cpu usage and comparing with a predefined value i.e threshold value. But my if condition is always going wrong. Please help me to correct my script.

#checking the cpu usage
cpu_usage=$( mpstat | awk '$12 ~ /[0-9.]+/ { print 100 - $12 }')
if [ $cpu_usage > $cpu_threshold ] ; then
echo "The CPU utilization is above threshold : $cpu_usage %"
echo "The CPU utilization is below threshold : $cpu_usage %"

Please do check the above script in bash beacause I have tried many ways but always if condition is giving wrong output.

Answer Source

@Ankit: Try: changing if [ $cpu_usage > $cpu_threshold ] to if [[ $cpu_usage > $cpu_threshold ]]

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download