Suppose you run some script such as a perl or tcl. For example, $ ./script.sh; ./script.pl; source script.tcl. It takes some time to get executed and return to prompt. How to display the percentage of execution meanwhile the execution?

Below you can find an example UI. It displays the execution time taken for the script at the end.

1% Completed the exectuion of script.pl
2% Completed the exectuion of script.pl
. . . .
100% Completed the the exectuion of script.pl

Ex: Execution time for script.pl is :50 Seconds
  • 5
    Since different scripts take a different amount of time to execute (and the same script will take different amounts of time on each execution), this doesn't make sense. You can only have a "percentage" of a known outcome (time to completion, data left until complete, etc). "When the script is complete" has no meaning. – Chris Down Feb 25 '12 at 14:24
  • 1
    You can use the time command to get the execution time of the script, like time ./script.sh. – ByteNudger Feb 25 '12 at 15:27
  • Good idea, Thanks!! but what about % Of execution – user1228191 Feb 25 '12 at 15:48
  • 7
    @user1228191 As Chris Down has already pointed out, you first need to know what 100% is before you know what 1% is. Whatever incremental steps you have in each script, you must have a method of determining, ahead of time, how many incremental steps there are in total. Then you have something to work with (on a per script basis), but until then you are looking for magic to happen... all progress bars, etc. work only because something about the final target is know ahead of time. – Peter.O Feb 25 '12 at 16:27
  • Yes Peter.O, i really missed out this basic point by my one way thinking!! – user1228191 Feb 25 '12 at 16:56

The following script shows:

  1. Current item (iteration) number
  2. Sliding window average rate (eg. average over 10 iterations)
  3. Overall average rate
  4. The percentage progressed so far (if the number of irrigation is known)

The output:

317:  window: 3.76/s   overall: 3.28/s   progress: 31.7%

The test file:

set test_file
# create a test file
for i in {1..1000} ;do echo $i; done >"$1"

The script:

if="$1"             # the input file
lct=$(wc -l <"$if") # number of lines in input file
tot=${lct:-0}       # total number of itterations; If unknown, default is 0
                    #+  The total is know in this case. '$tot' is just a rough 
                    #+  example of how to suppress the progress %age output
beg=$(date +%s.%N)  # starting unix time.%N is nanoseconds (a GNU extension)
swx=10              # keep a sliding window of max 'n' itteratons (to average)
unset sw            # an array of the last '$swx' rates
for i in $(seq 1 $lct) ;do
    sw[$i]=$(date +%s.%N)  # sliding window start time
    # ================                   
      sleep .$RANDOM       #  ... process something here
    # ================
    now=$(date +%s.%N)     # current unix time
    if ((i<=swx)) ;then
        sw1=1              # first element of sliding window  
        sww=$i             # sliding window width (starts from 1)
    bc=($(bc <<<"scale=2; $i/($now-$beg); $sww/($now-${sw[$sw1]})"))
    oavg=${bc[0]}                  # overall average rate
    swhz=${bc[1]}                  # sliding window rate
    ((i>swx)) && unset sw[$sw1-1]  # remove old entry from sliding window list
    ((tot==0)) && pc= || pc="progress: $(bc <<<"scale=1; x=($i*100)/$tot; if (x<1) print 0; x")%"
    msg="$i:  window: $swhz/s   overall: $oavg/s   $pc"
    printf "\r%"$((${#i}+1))"s=\r%s" "" "$msg"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.