I am trying to print a progress bar in shell as follows
#!/bin/bash
sleep $1 & PID=$!
printf "["
counter=1
while kill -0 $PID 2> /dev/null; do
percent_done=$(echo "${counter}*100/${1}" | bc -l )
printf "▓%.0f" $percent_done
sleep 1
counter=$counter+1
done
printf "] done!"
and i call it as
myScript.sh 10
so the script will invoke a sleep of 1 sec for 10 seconds. On each iteration of the loop, i want to print
▓<the percentage of the loop completed>
for instance i expect
▓10▓20▓30▓40▓50▓60
and so on. but i get
▓10▓11▓12▓13▓14▓15
instead
what am i doing wrong? am i not updating the counter properly? also, if i do
percent_done=$(echo "${counter}/${1}" | bc -l )
then i get
▓0.1000000000000▓1.100000000000000000▓2.10000000000000000000▓3.10000000000000000000▓
and so on.
Since I dont want the long floating point value, i added the *100
in the expression.
What am i doing wrong?