Wednesday, January 12, 2005

It recently came to my attention that bash numerical comparison operators "-lt -gt -eq -ne", are not capable of comparing non integer numbers. As a result the statement:

one=1.2
two=2.1
if [ $one -lt $two ]; then
echo "One is less than two";
fi

The way to deal with this problem is to make use of ascii comparison although I'm not entirely certain this solves all cases it worked for the case I was trying to use it under.

Your ending statement would look like

one=1.2
two=2.1
if [[ "$one" < "$two" ]]; then
echo "One is less then two"
fi

You need make use of the double [[ or escape the comparison operator as with a single [ the comparison operator is interpreted by the shell so you would end up with a syntax error.

0 Comments:

Post a Comment

<< Home