comparison of integer and floating point numbers i

2020-06-06 07:25发布

问题:

In shell script how can we compare (integer and floating point) ,(flaoting point and floating point),(floating point and integer),(integer and integer) with only one if condition.

i have few examples like

 set X=3.1
  set Y=4.1
  if [ $X < $Y ] then
    echo "wassup"
  endif

But running the above from cron job doesnt seem to work.

回答1:

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.



回答2:

Bah itself only handles integers. Use bc:

echo "$X>$Y" | bc 
0
echo "$X<$Y" | bc 
1

You don't need to worry about scale. It is just for the preocision of output formats:

X=3.000001
Y=3.0001
echo "$X>$Y" | bc 
0
echo "$X<$Y" | bc 
1
echo "scale=1;$X<$Y" | bc 
1


回答3:

below example works on bash shell.

 X=3.1
 Y=4.1
 if [ $X -le $Y ]
 then
    echo "wassup"
 fi

you may want to learn shell script here



回答4:

EDIT. based on the comments to this answer (thanks to user unknown and glenn jackman), it seems that when using bc for a true/false test, the required bash test is simply:

  • (( $(echo "$X < $Y" |bc) )) ... see the test results and script below

wheras, the comparison to -ne 0 is needed for the old style bash [ ] test.


bash does not natively handle floating point numbers, but you can call a utility such as bc

From man bc - An arbitrary precision calculator language

X=3.1
Y=4.1
# This test has two superfluous components. 
# See EDIT (above) and TESTS below
if (($(echo "scale=9; $X < $Y" |bc)!=0)) ;then
    echo "wassup"
fi

TEST results:

if [  "1"  ]   true
   [  "1"  ]   true
if [  "0"  ]   true
   [  "0"  ]   true

if [   1   ]   true
   [   1   ]   true
if [   0   ]   true
   [   0   ]   true

if (( "1" ))   true
   (( "1" ))   true
if (( "0" ))   false
   (( "0" ))   false

if ((  1  ))   true
   ((  1  ))   true
if ((  0  ))   false
   ((  0  ))   false

echo "1<1"|bc  true
echo "1<0"|bc  true

TEST script:

printf 'if [  "1"  ]   '; if [ "1" ]; then echo true; else echo false; fi
printf '   [  "1"  ]   ';    [ "1" ]  &&   echo true  ||   echo false
printf 'if [  "0"  ]   '; if [ "0" ]; then echo true; else echo false; fi
printf '   [  "0"  ]   ';    [ "0" ]  &&   echo true  ||   echo false
echo  
printf 'if [   1   ]   '; if [  1  ]; then echo true; else echo false; fi
printf '   [   1   ]   ';    [  1  ]  &&   echo true  ||   echo false
printf 'if [   0   ]   '; if [  0  ]; then echo true; else echo false; fi
printf '   [   0   ]   ';    [  0  ]  &&   echo true  ||   echo false
echo  
printf 'if (( "1" ))   '; if (("1")); then echo true; else echo false; fi
printf '   (( "1" ))   ';    (("1"))  &&   echo true  ||   echo false
printf 'if (( "0" ))   '; if (("0")); then echo true; else echo false; fi
printf '   (( "0" ))   ';    (("0"))  &&   echo true  ||   echo false
echo  
printf 'if ((  1  ))   '; if (( 1 )); then echo true; else echo false; fi
printf '   ((  1  ))   ';    (( 1 ))  &&   echo true  ||   echo false
printf 'if ((  0  ))   '; if (( 0 )); then echo true; else echo false; fi
printf '   ((  0  ))   ';    (( 0 ))  &&   echo true  ||   echo false
echo
printf 'echo "1<1"|bc  '; echo "1<1"|bc >/dev/null  && echo true || echo false 
printf 'echo "1<0"|bc  '; echo "1<0"|bc >/dev/null  && echo true || echo false 


标签: linux shell unix