0

I have a bash script that ends as follows:

trap "exit" INT
for ((i=0; i < $srccount; i++)); do
    echo -e "\"${src[$i]}\" will be synchronized to \"${dest[$i]}\""
    echo -e $'Press any key to continue or Ctrl+C to exit...\n' 
    read -rs -n1
    #show_progress_bar()
    rsync ${opt1} ${opt2} ${opt3} ${src[$i]} ${dest[$i]}
done

I need a command or a function such as show_progress_bar() that put . (a dot) in the stdout every one second while rsync command is running (or a rotating / that rotates as / - \ | sequence while rsync is running). Is it possible? Do I need to wrote such function myself, or there is available scripts for this purpose?

PHP Learner
  • 691
  • 1
  • 6
  • 14

1 Answers1

0

It's not pretty, but it works:

~$ while true; do echo -n .; sleep 1; done & sleep 3; kill %-; wait; echo;
[1] 26255
...[1]+  Terminated              while true; do
    echo -n .; sleep 1;
done

(exchange the "sleep 3" for your actual work)

It works like this:

  • The while loop runs as a background job.
  • Meanwhile, your work ("sleep 3" in my example) runs in the foreground.
  • When the work is done, "kill %-" kills the echo loop.
  • Then we wait for the job to terminate, and echo a newline, just in case.

Like I said, it's not pretty. And there's probably a much better way to do it. :)

EDIT: For example, like the answer here: Using BASH to display a progress (working) indicator

Community
  • 1
  • 1
Snild Dolkow
  • 6,669
  • 3
  • 20
  • 32