場景概述:執行280萬條sql,全是update,每執行一條,將輸出結果輸出到日誌裏。
要求:輸出當前執行進度,百分比顯示。
思路:進度需要知道總量和當前執行的量。總量獲得方式:
total=`grep update a.sql|wc -l`
當前執行量獲得方式:
current=`grep updated a.log|wc -l`
bash中浮點數運算的命令爲
#scale表示保留小數位數
result=`echo `scale=2;($current/$total)*100`|bc`
爲了使百分數總是覆蓋之前的,需要使用的命令是
#-n表示不換行,-e表示解析後邊的特殊字符,而\b表示退格,即刪除之前的字符,可以多寫幾個
echo -ne '\b\b\b\b\b\b'
綜合以上的命令,進行循環輸出顯示的代碼如下:
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