In the below code, ShellCheck throws an error in the while
clause.
count=10.0
while [ $count -le 20.0 ]
do
echo "Hello"
count=$(bc<<< "scale=4; (count+0.1)")
done
ShellCheck says:
Decimals not supported, either use integers or bc
I am not quite sure how to use bc in a while
loop.
while [ $(bc <<< "scale=4; (count -le 20.0)" ]
How do I compare decimal numbers in a while clause? Any advice?
Bash doesn't support floating point arithmetic. You can either use bc:
count="10.0"
limit="12.0"
increment="0.1"
while [ "$(bc <<< "$count < $limit")" == "1" ]; do
echo "Hello"
count=$(bc <<< "$count+$increment")
done
or awk:
while awk 'BEGIN { if ('$count'>='$limit') {exit 1}}'; do
echo "Hello"
count=$(bc <<< "$count+$increment")
done
I just wonder: why not (directly) count from 10.0 to 12.0 ?
for i in $(seq 10.0 0.1 12.0); do
echo "Hello"
done