您的位置:首页 > 其它

tar解压过程中在console显示简易进度条

2016-06-07 15:09 211 查看
本脚本用于在tar命令解压文件过程中,在console中显示简易的进度条,是根据解压出的文件总的大小和当前已经解压的文件大小来进行进度的显示,程序比较简易,普适性不是太强,很多变量可以以参数形式传递,这里不加优化,仅供作为笔记和参考而已

#!/bin/sh

#显示进度的总格数
total_process=100
#当前进度格数
current_process=0
#百分比
percent=0
#文件大小
total_size=455000
#当前文件大小
current_size=0
#用于记录上一次的进度
old_process=0

print_progress ()
{
incomplete_process=$((${total_process}-${current_process}))
if [ ${incomplete_process} -lt 0 ]; then
incomplete_process=0
current_process=${total_process}
percent=100
fi
#输出已升级部分,用>表示
printf "\rProgress:[%.${current_process}d" | tr '0' '>'
#输出未升级部分,用' '表示
printf "%.${incomplete_process}d]" | tr '0' ' '
#输出当前百分比
printf "${percent}%%"
}

display ()
{
while true
do
#统计当前已解压文件大小
current_size=$(du -s target | awk '{printf $1}')
percent=$((${current_size}/(${total_size}/100)))
#       echo ${percent}
current_process=$((${percent}\*${total_process}/100))
#       echo ${current_process}
if [ ${current_process} -eq 0 ]; then
print_progress
elif [ ${current_process} -ne ${old_process} ]; then
print_progress
old_progress=${current_process}
fi
#以tar进程结束来结束本进程
ps -ef | grep "tar jxvf" | grep -v "grep" > /dev/null
if [ $? -ne 0 ]; then
echo "Update Over"
break
fi
done
}

tar jxvf file.tar.gz -C ./target > /dev/null &
display
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: