Differences between revisions 2 and 7 (spanning 5 versions)
Revision 2 as of 2007-06-18 19:59:25
Size: 1015
Editor: GreyCat
Comment:
Revision 7 as of 2008-11-22 21:43:32
Size: 1453
Editor: GreyCat
Comment: first-line
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
[[Anchor(faq44)]] <<Anchor(faq44)>>
Line 3: Line 3:

The easiest way is to use {{{dialog --gauge}}}. Here is an example, which relies heavily on BASH features:
The easiest way is to use {{{dialog --gauge}}}. Here is an example, which relies heavily on [[BASH]] features:
Line 7: Line 6:
   # We want to process all of the *.zip files in the current directory.    # Bash
# Process all of the *.zip files in the current directory.
Line 18: Line 18:
 * 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.)
 * An [[BashFAQ/005|array]] named {{{files}}} is populated with all the files we want to process.
 * {{{dialog}}} is invoked, and its input is redirected from a ProcessSubstitution. (A pipe could also be used here; we'd simply have to reverse the {{{dialog}}} command and the loop.)
Line 23: Line 23:
For more examples of using {{{dialog}}}, see [#faq40 FAQ #40]. For more examples of using {{{dialog}}}, see [[BashFAQ/040|FAQ #40]].

A progress bar can also be programmed without any external commands. This example assumes a terminal that uses standard ISO 6429 (a.k.a. ANSI or VT100) escape sequences:

{{{
files=(*)
width=$COLUMNS

n=${#files[*]}; i=0
for f in "${files[@]}"; do
  # process "$f" in some way (for testing, "sleep 1")
  printf "\e[40m\e[7m%$(($width*$i/$n))s\n\e[A" " "
  i=$(( $i + 1 ))
done
printf "\e[0m\n"
}}}

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:

   # Bash
   # 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 ProcessSubstitution. (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 FAQ #40.

A progress bar can also be programmed without any external commands. This example assumes a terminal that uses standard ISO 6429 (a.k.a. ANSI or VT100) escape sequences:

files=(*)
width=$COLUMNS

n=${#files[*]}; i=0
for f in "${files[@]}"; do
  # process "$f" in some way (for testing, "sleep 1")
  printf "\e[40m\e[7m%$(($width*$i/$n))s\n\e[A" " "
  i=$(( $i + 1 ))
done
printf "\e[0m\n"

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