Search code examples
bashshellcentosuptime

Checking if a float as a string is greater than another


I am attempting to write a bash script that parses out the load on a server, and that checks that value against a predefined numerical value. If the Load value on the server is higher than the predetermined value, an error is reported. For some reason I am having issues with my if statement here, where the BAIL variable is returning as the value of the LOAD variable.

#!/bin/bash
set -a

load=$(uptime | grep -ohe 'load average[s:][: ].*' | awk -F ',' '{ print $2 }' | bc -l)
maxload=$(1.00 | bc -l)
bail=$(echo "$load -gt $maxload" | bc )


if  [[ $bail ]]
then
  echo "LOAD TOO HIGH ON SERVER"
  echo "$load"
  echo "$bail"
  exit 255 
else
  echo "ACCEPTABLE LOAD ON SERVER"
  echo  "$load"
  echo "$bail"
fi

What am I doing wrong when I am calculating the BAIL variable to use in the if statement? I'm trying to just get a 1 or a 0 back.


Solution

  • For bc version 1.07.1, the syntax for the comparison is

    bail=$(echo "$load > $maxload" | bc )
    

    Also, an echo is missing

    maxload=$(echo "1.00" | bc -l)