Sunday, May 28, 2017
Copy Progress Bar Shell Script
Copy Progress Bar Shell Script
This script not replaces cp command. This is a sample script for practice purpose.
I am going to develop more elegant script, which fulfil most of the cp command strengths.
I will post complete version of the script as soon as possible,
I shall really appreciate all comments and suggestions to improve this script.
#!/bin/bash
#
# SCRIPT: ddcopy_progress.sh
# PURPOSE: This script is used to watch progress of copying.
#
# Note: I am using destination file as $$.temp .This is because,
# after testing you can easily delete temp files using
# rm -f *.temp
#############################################
# Arguments Checking #
#############################################
if [ $# -ne 1 ] # Look for exactly one parameter
then
echo "..............Usage Error........."
echo "Usage: $0 Sourcefil"
exit 1
fi
if [ ! -e $1 ] # Check source file exist or not
then
echo "File $1 not exist"
exit 1
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 $1`
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 color
# 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=$1 of=$$.temp 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"
# sleep 1 # Uncomment it for small files
done
printf "e[0m "
#echo -e "33[0m"
Output:
Screen Shot1:
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj4zxbZ209FxdOAgScu1ECChwuroHN8yz_kaRJp1mC2B7OpXhSEfxKt4GVaCT3XAaSkRu65Sy3tsKylSt9-6MmwHYtxaCpLt9qI74I9E9UaDh0aHB1Be2vzo-5kDiH8Wp8tMCDdA6hIF8o/s400/progress1.png)
Screen Shot2:
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjD9el_xt0_bRPvcY6uHUDIICLjlu36vkRnmUI8OuD9w-1XHyFGWoJ0OTxTkroSt_LAtwA7J8-AaakTkMOsLiqG88ho8dnUUPE4wT-2CLBhFSEFIZIshw-WyCBI2ly6ZlKKM2t0BltnKKM/s400/progress2.png)
Screen Shot3:
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh1NsCi2htPubMmsZem8YKWKyuCTxO6mZ_lN_xHisrZJdNqwzbUhpfsPApNtYg3affQmuquZ2XAKKkJVfFQ7jAc5EJZC7PbXPZY4PqRIc-5-yFTCJRwFZBMaefmYR7q-ufQ56JHzepSpWQ/s400/progress3.png)
Screen Shot4:
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgOC40lR4c6hOIjVWnQSZOLBe5WiAyQ1UYtj9XUnbTe1pLvXB12aRoxT3qxZlZXmfBzIyLdlp034_NHjg3eCNRms32BwJeA0l3yycQ8dTI6aMSbc3TWmoCUS6wWahkmGixxuShX5BKfLWk/s400/progress4.png)
Observation:
I tested above script with different block count.But I didnt get accurate result.
Some times less count script given good result, some times more block count given
good result.
I copied 1.8 GB Movie file with this script.With time command it has give bellow
result.
real 1m12.646s
user 0m0.043s
sys 0m7.854s
From my observation this script has taken less time compared with mouse copy and
paste method. Copy and paste has taken 1m14.02s averagely.
Available link for download