0

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"

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

It may help you !

import time

print "Copying files .."
time.sleep(1)
print "."
time.sleep(1)
print "."
time.sleep(1)
print ".."
time.sleep(1)
print ""
print "File copy complete"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.