progress_fancy.sh - randomcrap - random crap programs of varying quality
 (HTM) git clone git://git.codemadness.org/randomcrap
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
       progress_fancy.sh (870B)
       ---
            1 #!/bin/sh
            2 # Progress indicator script. Pass the total amount of lines (known beforehand)
            3 # as argument. Pass lines as input to stdin. Writes progress status to stderr.
            4 # Alternative: pv -s total -l
            5 
            6 total="$(($1 + 0))" # convert int
            7 if test "${total}" -le 0 -o "$1" != "${total}"; then
            8         echo "usage: $0 <total>" >&2
            9         exit 1
           10 fi
           11 
           12 LC_ALL=C awk -v "total=${total}" '
           13 # stupid hack to draw a small DOS-style progressbar (10 cells wide).
           14 function bar(pc) {
           15         s = "";
           16         pc /= 10;
           17         for (i = 0; i < pc && i < 10; i++)
           18                 s = s "\xe2\x96\x91"; # U+2593 DARK SHADE
           19         for (; i < 10; i++)
           20                 s = s "\xe2\x96\x93"; # U+2591 LIGHT SHADE
           21         return s;
           22 }
           23 {
           24         counter++;
           25         percent = (counter * 100) / total;
           26         printf("\033[K") > "/dev/stderr";
           27         print $0;
           28         printf("[%s/%s] %.0f%% %s\r", counter, total, percent, bar(percent)) > "/dev/stderr";
           29         fflush();
           30 }
           31 END {
           32         printf("\033[K") > "/dev/stderr";
           33 }'