satch_boogie satch_boogie - 6 months ago 18
Python Question

how to implement this code using loops in bash or python

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"

Answer

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