How to work with decimals in linux shell, conditional use if & else

0

I'm working on a code that allows to know the classification of a student through a note entered in shell linux, my problem is that I could not adjust the value to decimal places, if I use the "," the program runs but does not work good.

I attach my code

echo   Este programa muestra si aprobó o no un estudiante
echo "Ingrese nota a"
read numeroa


#-----------------Numero---perdio-----------------------

if (( $numeroa >= 1 && $numeroa <=2,9));
  then
   echo perdio
  fi

#-----------------Numero--Aprobó---------------------

if (($numeroa >= 3))
  then
   echo Aprobó
  fi


exit 0

Here is the error when working with decimals:

    
asked by Jesús Burgos 01.10.2018 в 20:01
source

1 answer

2

As he says Patricio Moracho bash does not support calculations with non-integer numbers (floating-point arithmetic or "with decimals")

If the precision requirement is a single-digit test by multiplying by 10, something like this:

read numeroa; 
if (( $numeroa * 10 >= 10 && $numeroa * 10 <= 29 )); 
  then echo "perdio";
fi

Another way is by using an external auxiliary program bc to do the comparison, something like this:

read numeroa;
if (( $(echo "$numeroa >= 1 && $numeroa <= 2.9" | bc -l) ));
 then echo "perdio";
fi

Bonus

If you want to use the , as a decimal point separator the 2,9 you filter it with another auxiliary program sed , something like this:

read numeroa;
if (( $(echo "$numeroa >= 1 && $numeroa <= 2,9" | sed -u 's/,/./g' | bc -l) )); 
  then echo "perdio";
fi
    
answered by 01.10.2018 / 20:33
source