cp_显示进度

#!/bin/bash

PARAM_LIST=$*
PARAM_NUM=$#
TOTAL_SIZE=0
for((i = 0; i < PARAM_NUM - 1; i++))
do
        [ ! -r $1 ] && echo "Cannot read $1." && exit 1
        SIZE=`du -s $1 | awk '{print $1}'`
        ((TOTAL_SIZE = TOTAL_SIZE + SIZE))
        shift
done
TARGET=$1
START_TIME=`date +%s.%N`
cp -a $PARAM_LIST &
echo -ne "Total size: $TOTAL_SIZE KB - %"
while true
do
        LEN_PERCENT=${#PERCENT}
        for((i = 0; i <= LEN_PERCENT; i++))
        do
                echo -ne "\b"
        done
        COPIED=`du -s $TARGET | awk '{print $1}'`
        ((PERCENT = COPIED * 100 / TOTAL_SIZE))
        echo -ne "$PERCENT%"
        ((PERCENT == 100)) && END_TIME=`date +%s.%N` && break
done
MB_SECOND=`echo $TOTAL_SIZE/1024/\($END_TIME-$START_TIME\)|bc`
echo " - SPEED: $MB_SECOND MB/s"


+++++++++++++++++++++++++++++++++++++++++++++++++++++

首先将如下脚本放置在/usr/bin下,

要使用cp指令時,以大写CP指令,代替小写cp. (当然你可以自己修改脚本定制适合自己的命令)

脚本內容:

#!/bin/bash

SOURCE=$1
TARGET=$2

#CP=./fack_cp
CP=cp

$CP "$SOURCE" "$TARGET" &
CPID=$!

isalive()
{
    out=`ps -p $1 2> /dev/null`
    return $?
}

while [ 1 ]; do
{
    SSIZE=`/bin/ls -l $SOURCE | awk "{print \\\$5}"`
    if [ -f $TARGET ]; then
        TSIZE=`/bin/ls -l $TARGET | awk "{print \\\$5}"`
    else
        TSIZE="0"
    fi
    PERCENT=`echo "scale=2; $TSIZE/$SSIZE*100" | bc -l`
    RATE=`echo "scale=0; 63*$PERCENT/100" | bc -l`
    BLUE="\\033[3;44m"
    NORMAIL="\\033[0;39m"

    BAR=$BLUE
    i=0
    while [ $i -le 62 ]; do
        [ $i = $RATE ] && BAR=$BAR"\\033[7;39m"
        BAR=$BAR" "
        let i=$i+1
    done
    BAR=$BAR$NORMAIL
    echo -en "\r$BAR ${PERCENT}%"
    if ! isalive "$CPID"; then echo -en "\n"; exit; fi
    sleep 1
}
done

分割线
感谢打赏
江西数库信息技术有限公司
YWSOS.COM 平台代运维解决方案
 评论
 发表评论
姓   名:

Powered by AKCMS