The problem with this script is that it uses floating point numbers,
which by default the bash shell cannot handle. In order to use it right
you will have to make the comparison with an external program (inside
your script) and pass the result of that comparison to the conditional
In order to do that you will have to use bc
, a programming
language calculator that can be used for such purposes.
What I do in the script is that I first read the input of the file
to a variable (a step that can be ommitted but is put here for clarity)
and then I compare the floating point of that variable
to whatever other number I want. The result of the comparison is 1 if
it is true or 0 if not. Then, I check the result if it is 1 or 0 and I
I also have a text file, named file1
, containing the number 10.02
, or 9.04
for testing purposes. The same name is used for the variable, but you can use any name.
Here is the code that you should use:
result=`echo "scale=2; $file1 > 10.02" | bc`
if [ $result -eq 1 ]
echo "greater than 10.02"
echo "not greater than 10.02"
command is used to tell bc
the number of
decimals to use after the point for the float. You can change that to
Gor more information on bc use man bc.
I hope that I have helped.