Skip to content
Advertisement

Comparing two numbers but if condition does not work correctly

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.

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

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

Advertisement

Answer

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

Advertisement