標籤:shell 進度條 tput
shell中列印進度條,需要用到tput、printf函數。
# tput row col 游標移到第row行第col列
# tput cols 擷取視窗最大列數
# tput lines 擷取視窗最大行書
# echo $LINENO 擷取游標當前行號
function getprostr()
{
pchar=$1
ptotal=$2
outstr=""
numcol=`tput cols`
coltotal=$[$numcol/2/$ptotal]
for((i=1;i<=$coltotal;i++));
do
outstr="$outstr$pchar";
done
echo $outstr
}
function progressHead()
{
outstr=""
ptotal=$1
row=$2
str=`getprostr "-" $1`
for((i=1;i<=$ptotal;i++));
do
outstr="$outstr$str";
done
tput cup $row 0
echo "$outstr-"
}
function progress()
{
tput sc
pvalue=$1
ptotal=$2
row=$3
numcol=`tput cols`
gstr=`getprostr "=" $ptotal`
str="$gstr>"
if [ $pvalue == $ptotal ]; then
str="$gstr="
fi
barcol=$[($pvalue-1)*(${#str}-1)]
percent=$[$pvalue*100/ptotal]
tput cup $row $barcol
echo "$str"
tput cup $row $[$numcol/2]
echo "[$percent%]"
tput rc
}
clear
cols=`tput cols`
rows=`tput lines`
curline=$LINENO
progressHead 5 $[$LINENO-$curline-2]
progressrow=$[$LINENO-$curline-2]
#echo ""
progressHead 5 $[$LINENO-$curline-3]
progress 1 5 $progressrow
sleep 1
progress 2 5 $progressrow
sleep 1
progress 3 5 $progressrow
sleep 1
progress 4 5 $progressrow
sleep 1
progress 5 5 $progressrow
輸出結果:
-----------------------------------------
========================================[100%]
-----------------------------------------
本文出自 “小何貝貝的技術空間” 部落格,請務必保留此出處http://babyhe.blog.51cto.com/1104064/1612009
shell進度條