Wednesday, September 03, 2008

comparing floating point numbers in bash

#!/bin/bash
# expr prints "1" on success, "0" on failure

echo "Enter two values, space seperated"
read val1 val2

result=`expr $val1 \> $val2`
if [ "$result" -eq "1" ]; then
echo "$val1 is larger than $val2"
exit 0
fi
result=`expr $val2 \> $val1`
if [ "$result" -eq "1" ]; then
echo "$val2 is larger than $val1"
exit 0
fi

echo "the values are equal"
exit 0

0 Comments:

Post a Comment

<< Home