Friday, December 6, 2013

Shell Script to show Progress with Dots while running some commands.

If you would like to go interactive scripts and if your requirement is to show some status to user while your script is running some other set of commands with some indicators like Dots or Hash symbol and whatever you want in Shell Script then you can use the following logic.

#!/bin/bash

STATUS() {
echo -n "Copy is in Progress, Please Wait"
while true
do
echo -n "."
sleep 2
done
}

STATUS &
STAT_PID=$!
# Please all your real commands here..
dd if=/dev/zero of=/tmp/test bs=1 count=0 seek=1G >/dev/null 2>&1 
                                 ## You can redirect output to a file.

kill -9 $STAT_PID
echo -e "\nCopy Completed!"

You can run the script and you can see dots comes for every 2s and also at the same time dd command will create 1G file. Instead of dd command you can use your own commands over here and customize as per your requirement.

# ./test_progress.sh
Copy is in Progress, Please Wait......
Copy Completed!
# ls -l /tmp/test
-rw-r--r-- 1 root root 1073741824 Dec  6 23:31 /tmp/test
# ls -lh /tmp/test
-rw-r--r-- 1 root root 1.0G Dec  6 23:31 /tmp/test