Comparison of Integer and Floating Point Numbers in Shell Script

comparison of integer and floating point numbers in shell script

The way to carry out floating point operations in bash is to use bc which is available on almost all linux distributions.

# bc will return 0 for false and 1 for true
if [ $(echo "23.3 > 7.3" | bc) -ne 0 ]
then
echo "wassup"
fi

There's a good article available on linux journal about floating point math in bash using bc.

Comparing floating-point numbers in bash

Normally, you'd need to use something other than native shell math, as described in BashFAQ #22. However, since you're comparing to integers, this is easy: You can just truncate at the decimal point.

[ "${UPTIME%%.*}" -gt 600 ] # truncates your UPTIME at the decimal point
[ "${WAIT%%.*}" -gt 50 ] # likewise

Comparing an integer and floating point in bash

This will work

#!/bin/bash
volume=4.189
if [[ $(echo "$volume == 4.189" | bc) -eq "1" ]]; then
echo Equal
else
echo Not Equal
fi

or simply put the literal in quotes

#!/bin/bash
volume=4.189
if [[ $volume == "4.189" ]]; then
echo Equal
else
echo Not Equal
fi

Notice that of the two ways I showed to compare floating point the preferred is to use bc, it will tell you that 4.1890 is equal to 4.189 whereas the second method is a dumb string compare, they will compare unequal.

Floating point comparison in shell

bash doesn't do floats, use awk

key1=12.3
result=12.5
var=$(awk 'BEGIN{ print "'$key1'"<"'$result'" }')
# or var=$(awk -v key=$key1 -v result=$result 'BEGIN{print result<key?1:0}')
# or var=$(awk 'BEGIN{print "'$result'"<"'$key1'"?1:0}')
# or
if [ "$var" -eq 1 ];then
echo "do something"
else
echo "result more than key"
fi

there are other shells that can do floats, like zsh or ksh, you might like to try using them as well

floating point number comparison in bash script

If you want to use bc use it like this:

if [[ $(bc -l <<< "$chi_square>3.84") -eq 1 ]]; then
echo 'yes'
else
echo 'no'
fi

Shell scripting - Comparing floating number within if then else

bash can not do floating point arithmetics.

You can use a more advanced shell like zsh:

% [[ 1.1 -gt .45 ]] && echo 'Ok'
Ok

Or use bc to do the comparison:

$ echo '1.1 > .45' | bc -l
1

bc returns 1 if true and 0 if false.

Comparing float values in bash

In bash, you need to be very careful about spacing. For example:

if [ $(echo " 0.5 > $X " | bc -l )==1 ]; then
echo grande
fi

Here, there are no spaces around the ==, so it'll be interpreted as:

if [ 0==1 ]; then
fi

Believe it or not, this condition is always true.

Consider:

if [ "$(echo " 0.5 > $X " | bc -l )" == 1 ]; then
echo grande
fi

.

How to compare two floating-point values in shell script

You can compare floating-point numbers using expr(1):

: nr@yorkie 3724 ; expr 3.1 '<' 3.3
1
: nr@yorkie 3725 ; expr 3.1 '<' 3.09
0

You can also have bc do the comparisons as well as the calculations:

if [ "$(echo $result1 '<' $result2 | bc -l)" -eq 1 ];then ... fi

Finally, ksh93 can do arithmetic evaluation $(($result1 < $result2)) with floating-point numbers, although bash cannot.



Related Topics



Leave a reply



Submit