cp显示进度百分比

2565阅读 0评论2012-04-29 ulovko
分类:BSD


  1. #!/bin/bash
  2. trap 'exit_fun' 2

  3. usage(){
  4.         echo "Usage: `basename $0` \"src file\" \"dst file\""
  5.         exit 1
  6. }

  7. exit_fun(){
  8.         echo -e "\033[?25h"
  9.         kill -9 $(ps -ef|awk '/ [c]p /{print $2}') &>/dev/null
  10.         exit 1
  11. }


  12. [ "$#" -ne "2" ] && usage
  13. [ -d "$2" ] && k=${2%%/}/`basename $1` || k=$2

  14. fromsize=`ls -s $1|cut -d" " -f1`
  15. cp $1 $k &
  16. start=`date +%s`
  17. sleep 0.2
  18. echo -ne "\033[?25l"
  19. while :;do
  20.         tosize=`ls -s $k|cut -d" " -f1`
  21.         x=`echo "$tosize $fromsize"|awk '{print int($1*100/$2)}'`
  22.         [ $x -eq 99 ] && x=100
  23.         echo -n "["
  24.         for((i=0;i<x;i=$i+2));do echo -n "=";done
  25.         echo -n ">"
  26.         for((j=i;j<=99;j=$j+2));do echo -n " ";done
  27.         now=$[`date +%s` - $start]
  28.         [ $now -eq 0 ] && now=1
  29.         avg=$[$tosize/$now/1024]
  30.         printf "] %4s%% %4sM/s" $i $avg
  31.         [ $x -ge 100 ] && break
  32.         sleep 0.1
  33.         echo -ne "\r"
  34. done
  35. echo -e "\033[?25h"

FROM: 
上一篇:使用ZFS的十条理由
下一篇:FreeBSD recover a lost root password