dimanche 4 septembre 2016

Compare a float with a defined number in shell

I have the following script, which I want to use to compare a float to another given float.

#!/bin/sh
echo 'Enter a real number'
read n
echo n=$n

if (( $(echo "$n > 0.0" |bc -l) ))
then
    echo 'n is +ve'
elif (( $(echo "$n < 0.0" |bc -l) ))
then
    echo 'n is -ve'
else
    echo 'n is zero'
fi

This works fine in my OS X, however the if statement shows error ("not found") in Linux (Ubuntu).

What could be the more universal syntax ? (I guess one can still work with bc for floats).

Aucun commentaire:

Enregistrer un commentaire