Output percentage progress under shell

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/cheng_870701/article/details/102767274

Scene overview: Perform 2.8 million sql, full update, execute each one, will output the results to a log.

Requirements: output current execution progress, displayed as a percentage.

Ideas: progress and need to know the total amount currently being executed. The total amount of availability:

total=`grep update a.sql|wc -l`

The current implementation of the amount obtained by:

current=`grep updated a.log|wc -l`

bash floating-point operation command

#scale表示保留小数位数
result=`echo `scale=2;($current/$total)*100`|bc`

In order to make the percentage is always covered before, is the need to use the command

#-n表示不换行,-e表示解析后边的特殊字符,而\b表示退格,即删除之前的字符,可以多写几个
echo -ne '\b\b\b\b\b\b'

Based on the above commands, circulated output display code is as follows:

while :
do
sleep 1
total=`grep update a.sql|wc -l`
current=`grep updated a.log|wc -l`
result=`echo `scale=2;($current/$total)*100`|bc`
echo -ne "\b\b\b\b\b\b${result}%"
if [ $result -eq 100 ];then
 break
fi
done

 

Guess you like

Origin blog.csdn.net/cheng_870701/article/details/102767274