Below code basically prints a dot "." where Im trying to imitate a progress bar in shellscript
#!/bin/bash
printf "Copying files .."
sleep 1
printf "."
sleep 1
printf "."
sleep 1
printf ".."
clear
sleep 1
echo ""
echo "File copy complete"
In Python you could do something like this:
from time import sleep
from sys import stdout
Print = stdout.write
Print("Copying files..")
for x in xrange(4):
Print(".")
sleep(1)
print "\nFile copy complete."
On each iteration it prints a new .
After a quick search I found this article which gives a good explanation of how to copy a file/directory whilst updating a progress bar.
There are so many ways to write a loop. I kind of like:
yes | sed 5q | while read r; do sleep 1; printf .; done; echo
But you really don't want a loop; you want to keep printing the progress bar until the copy finishes, so you want something like:
progress() { while :; do sleep 1; printf .; done; }
copy_the_files & # start the copy
copy_pid=$! # record the pid
progress & # start up a process to draw the progress bar
progress_pid=$! # record the pid
wait $copy_pid # wait for the copy to finish
kill $progress_pid # terminate the progress bar
echo
or perhaps (where you should replace 'sleep 5' with commands to copy your files)
#!/bin/bash
copy_the_files() { sleep 5; kill -s USR1 $$; }
progress() { while :; do sleep 1; printf .; done; }
copy_the_files &
progress &
trap 'kill $!; echo' USR1
wait