Differences between revisions 1 and 2
Revision 1 as of 2007-05-02 23:34:56
Size: 35
Editor: redondos
Comment:
Revision 2 as of 2007-06-18 19:59:25
Size: 1015
Editor: GreyCat
Comment:
Deletions are marked like this. Additions are marked like this.
Line 2: Line 2:
== Removed. == == How do I create a progress bar? ==

The easiest way is to use {{{dialog --gauge}}}. Here is an example, which relies heavily on BASH features:

{{{
   # We want to process all of the *.zip files in the current directory.
   files=(*.zip)
   dialog --gauge "Working..." 20 75 < <(
      n=${#files[*]}; i=0
      for f in "${files[@]}"; do
  # process "$f" in some way (for testing, "sleep 1")
  echo $((100*(++i)/n))
      done)
}}}

Here's an explanation of what it's doing:
 * An array named {{{files}}} is populated with all the files we want to process.
 * {{{dialog}}} is invoked, and its input is redirected from a process substitution. (A pipe could also be used here; we'd simply have to reverse the {{{dialog}}} command and the loop.)
 * The processing loop iterates over the array.
 * Every time a file is processed, it increments a counter ({{{i}}}), and writes the percent complete to stdout.

For more examples of using {{{dialog}}}, see [#faq40 FAQ #40].

Anchor(faq44)

How do I create a progress bar?

The easiest way is to use dialog --gauge. Here is an example, which relies heavily on BASH features:

   # We want to process all of the *.zip files in the current directory.
   files=(*.zip)
   dialog --gauge "Working..." 20 75 < <(
      n=${#files[*]}; i=0
      for f in "${files[@]}"; do
         # process "$f" in some way (for testing, "sleep 1")
         echo $((100*(++i)/n))
      done)

Here's an explanation of what it's doing:

  • An array named files is populated with all the files we want to process.

  • dialog is invoked, and its input is redirected from a process substitution. (A pipe could also be used here; we'd simply have to reverse the dialog command and the loop.)

  • The processing loop iterates over the array.
  • Every time a file is processed, it increments a counter (i), and writes the percent complete to stdout.

For more examples of using dialog, see [#faq40 FAQ #40].

BashFAQ/044 (last edited 2017-11-13 22:19:11 by GreyCat)