the problem is in this line
elif [ "$(echo $a '>' $v06 | bc -l)" -eq 1 && "$(echo $a '<' $v08 | bc -l)" -eq 1 ];then
How can I test if the number is in range ? I know that there might be other solution for this but I want to solve in this way with bc...
a=0.1
v06=0.6
v08=0.8
if [ "$(echo $a '<' $v06 | bc -l)" -eq 1 ];then
echo " <0.6"
elif [ "$(echo $a '>' $v06 | bc -l)" -eq 1 && "$(echo $a '<' $v08 | bc -l)" -eq 1 ];then
echo " <0.6 >0.8"
else
echo ">1.5"
fi
If you really want to use bc:
I recommend to use braces around a variable (so you are sure 06 and 08 won't be append to
$v
), use double brackets in your tests if you are using operators like&&
.Actually the only thing wrong is you're using
&&
inside[ ]
which does not work.Only
[[ ]]
can contain&&
for multiple test conditions.You can put the
&&
outside instead, like thisor even better,
since
bc
actually supports the(num1 > num2) && (num1 < num3)
notationp/s: You don't need the quotes around the
$( )
since you're only expecting0
or1
as return valuep/s2: You can have quotes within
$( )
even if it's enclosed in quotes e.g.you can strip the "0." and use the -gt and -lt operators: