I am trying to use bc to compare a value with decimals in my if statement.
I am not sure if I am using bc correctly in my if statement
because it doesn't seem to work. It always enters the 4th if statement
"IF4
". It doesn't seem like the other conditions are being read properly?
#SET INITIAL SPEED
initialspeed() {
wpm=0
echo wpm: $wpm
#CHECKS IF SPEED WAS GIVEN
if [ -z "$wpm" ]
then
let wpm=0.6
echo IF1 wpm: $wpm #DEBUG
#CHECK IF SPEED IS VALID
elif [ 'echo "if$($wpm > 0.6)1;if$($wpm<=0.6)0" | bc' = 1 ]
then
echo IF2 wpm: $wpm #DEBUG
echo Error speed is less than 100 wpm
exit 3
elif [ 'echo "if$($wpm < 0.06)1;if$($wpm<=0.06)0" | bc' = 1 ]
then
echo IF3 wpm: $wpm #DEBUG
echo Error speed is more than 1000 wpm
exit 3
else
wpm=$(echo "scale=2;60/$2 | bc")
echo IF4 wpm: $wpm #DEBUG
fi
}
EDIT: I am actually trying to write a script similar to the "spritz" speed reader. $1 is the file to be read and $2 is the speed that the words will be displayed to the screen. $2 is am optional argument if its not given I set a default speed.
The speed given should be between 100 and 1000 wpm (0.6 and 0.06 before the calculation)