I wonder how other people would approach this. I often have to compare
two values when writing scripts and one or both of these values might
be an expression. I wrote a script to do it but I can't help thinking
there might be a much easier way. I am assuming the bash shell.
#!/bin/bash if [ $# -ne 2 ] ; then # recalculate just in case they are fractions or expressions # compare within gawk
# Script : compfl
# Version : 1.0
# Author : Roland Rashleigh-Berry
# Date : 03-Nov-2004
# Purpose : To compare two floating point numbers
# SubScripts : none
# Notes : Will work with integers and also fractions and
expressions.
# Returns GT, EQ or LT.
# It will calculate fractions or expressions to 9 decimal
places
# Usage : compfl 1.2 3.4
# compfl 355/113 3.14159
# compfl 3*1.33 3.99
# if [ "$(compfl 1.2 3.4)" == "LT" ] ; then
# print "less than"
# fi
#==========================================================================
# PARAMETERS:
#-pos- -------------------------------description--------------------------------
# 1 First number (or expression) to compare
# 2 Second number (or expression) to compare
#==========================================================================
# AMENDMENT HISTORY:
# init --date-- mod-id
----------------------description-------------------------
#
#==========================================================================
echo "Usage: compfl 6.67 4.45" 1>&2
exit 1
fi
num1=$(echo "scale=9; $1 + 0" | bc -l)
num2=$(echo "scale=9; $2 + 0" | bc -l)
echo | gawk '{
if (num1 > num2)
print "GT"
else
{
if (num1 == num2)
print "EQ"
else
print "LT"
}