I am writing a simple bash script to calculate a square root to 3 decimal places by default but the user can set the number of places... Nothing complex, I just iterate from 1 to the lowest square root. Since my bash is still basic this is what I came up with.
#!/usr/bin/env bash
num=${1}
places=${2-3}
i=0
while [[ $(( i*i )) -lt ${num} ]] // <= The problem should be here
do
i=$(( i + 1 ))
done
echo ${i};
rem=$(( num % i ))
root="${i}."
for (( j=0; j<places; j++ ))
do
rem=$((rem * 10))
root="$root$((rem / i))"
rem=$((rem % i))
done
echo ${root}
But due to some reason, it won't produce the correct result for a wide range of numbers
like bashfile.sh 9 // will produce 3.000 but bashfile.sh 8 will return 3.666
kindly help, what is wrong with while [[ ]]