程序源码
# 使用示例:progress 1 100
# 参数1:当前数值
# 参数2:总数值
progress () {
pro=`awk 'BEGIN{printf "%d",('$1'/'$2')*100}'`
spaces=`seq -s '#' $((${pro} +1)) | sed 's/[0-9]//g'`
if [ "${pro}" != "100" ]; then
printf "[%-100s] %d%% \r" "${spaces}" "${pro}"
else
printf "[%-100s] %d%% \n" "${spaces}" "${pro}"
fi
}
测试用例
#!/bin/bash
# test.sh
# 使用示例:progress 1 100
# 参数1:当前数值
# 参数2:总数值
progress () {
pro=`awk 'BEGIN{printf "%d",('$1'/'$2')*100}'`
spaces=`seq -s '#' $((${pro} +1)) | sed 's/[0-9]//g'`
if [ "${pro}" != "100" ]; then
printf "[%-100s] %d%% \r" "${spaces}" "${pro}"
else
printf "[%-100s] %d%% \n" "${spaces}" "${pro}"
fi
}
for i in `seq 1 100`
do
progress ${i} 100
sleep 0.01
done
运行结果
$ sh test.sh
[####################################################################################################] 100%