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

本脚本用于在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


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值