Thursday, April 20, 2017

Copy Progress Bar Shell Script V2

Copy Progress Bar Shell Script V2


This is the upgrade version of the ddcopy_progress.sh script posted on 2nd November.

#!/bin/bash
#
# SCRIPT: ddcopy.sh
# PURPOSE: Copies files and shows the progress of copying.
#
#............................USAGE...........................
# This script copies only files not directories
# ddcopy <Source filename or Path> <Destination filename or Path>
# ddcopy <Source filename or Path> <Destination Directory or Path>


##################################################################
# Arguments Checking #
##################################################################

if [ $# -eq 0 ] # Look for parameters supplied or not
then
echo "$0: missing file operand"
exit 1
fi

if [ $# -eq 1 ] # Look for exactly one parameter
then
echo "$0 : missing destination file operand after $1"
exit 1
fi

if [ $# -eq 2 ]
then
Source="$1"
Dest="$2"
if [ ! -e "$1" ] # Check source file exist or not
then
echo "$0: cannot stat $1: No such file or directory"
exit 1
elif [ -d "$1" ]
then
echo "$0: Source file $1 is a directory"
exit 1
fi

egrep -q "^/|/$" <<<$2

if [ $? -eq 0 -a ! -d $2 ]
then
echo "$0: cannot create regular file $2 : Is a directory"
exit 1
fi

if [ -d "$2" ]
then
filename=`basename "$1"`
$(egrep -q "/$" <<<$2) && Dest="$2$filename" || Dest="$2/$filename"
fi

if [ -e "$Dest" -a -f "$Dest" ]
then
echo -n "$0: $Dest already exist : overwrite $Dest (y/n)? "
read answer
if [ "$answer" = "n" -o "$answer" = "N" ]
then
exit 1
fi
fi
fi

###################################################################
# DEFINE VARIABLES HERE #
###################################################################

Seek=0 # skip BLOCKS bs-sized blocks at start of output
Skip=0 # skip BLOCKS bs-sized blocks at start of input
Bsize=128000 # block size
size=`stat -c %s "$Source"`

# Bellow 25MB files copied directly with copy command
# If you want progress bar for < 25MB files also, remove bellow
# if block

if [ $size -lt 25000000 ]
then
cp "$Source" "$Dest"
exit 0
fi

blocks=$((size/Bsize)) # total blocks of input file

lastblock=$((size%Bsize)) # last block, which size < $Bsize

# If last block size is > 0 then add 1 block to existing blocks.

if [ $lastblock -gt 0 ]
then
let blocks++
fi

# I am dividing screen width 60 as 20 parts.Each part is filled
# with a colour after each iteration.

# Make blocks count dividable by 20.

addblocks=$((blocks%20))
if [ $addblocks -gt 0 ]
then
adjustblocks=$((20-addblocks))
blocks=$((blocks+adjustblocks))
fi

Count=$((blocks/20))

# Count variable contain number of blocks to be copied for
# each iteration

###################################################################
# MAIN PROGRAM STARTS HERE #
###################################################################

printf "e[40me[43m%60s e[A" " "

for ((i=1;i<=20;i++))
do

dd if="$Source" of="$Dest" bs=128kB seek=$Seek skip=$Skip
count=$Count 2>/dev/null

Skip=$((Skip+$Count))
Seek=$((Seek+$Count))
j=$((j+3)) # 60/20 each part is 3 chars length

printf "e[40me[7m%${j}s e[A" " "
# echo -e "33[7m c"

done
printf "e[0m "
#echo -e "33[0m"

OUTPUT:
Screen Shot1:

Screen Shot2:

Screen Shot3:

Available link for download