Purpose: If you are into writing shell scripts and you wish you could display a nice little text progress bar as your shell scripts execute different commands, then your worries are over. In this blog entry we will see how we can implement a text progress bar in our shell scripts using a very handy utility called ‘clpbar‘. Some good reasons to have a progress bar are:

  • 1. Let user know how much more they have to wait before a particular task/command gets completed.
  • 2. Visually appealing and professional

and there are many more…

Step 1: Download or obtain the source

This utility (as far as I know) is not a part of any standard Linux distribution like Ubuntu, Fedora, Debian, etc. So we will download the utility for the sourceforge website. You will notice that the author has uploaded various formats of binary installation package: “.deb” package for Debian/Ubuntu based systems, “.rpm” for Fedora based systems and also the source tarball if you need to compile it. We will download the “.deb” package and will install on a Debian Lenny system.  Either you can manually download the file from your Internet browser or you can simply type this command on your Linux system:
# apt-get update
# apt-get wget
# wget http://downloads.sourceforge.net/clpbar/bar_1.10.9_i386.deb?modtime=1183036896&big_mirror=0

This will download the file into the directory from where you gave the above command.

Step 2: Install the “.deb” file

Once you have downloaded the “.deb” file on your Linux system, install it using dpkg command:
# dpkg -i <file-you-downloaded-from-step1>
# dpkg -i bar_1.10.9_i386.deb

Step 3:  Use it within your shell script or even with standalone along with any command

Once you install finish installation from Step 2, you will now have a binary called “bar”. For example simply type:
# bar
# t    0.0B/s   elapsed:   0:00:01

This shows that “bar” is installed properly. Now let’s see this command in some action. Suppose you want to untar a 500+ MB file (say image.tar.bz2) and you would like to see a progress bar and an estimated time of completion.  Just give the following command:
#bar image.tar.bz2 | tar xjf -

and you will be able to see something like this:
# bar image.tar.bz2 | tar xjf -
44.2MB at  383.1KB/s  eta:   0:05:20   26% [=========                        ]

The above output tells us the following:

ETA (estimated time of arrival):  5:20

How much is the transfer rate: 383.1KB/s

How much of the file is untar’d: 44.2MB

Note: Do not ignore the “-” sign at the end of command above, otherwise you will get an error message.

Step 4: Use in shell script

Using “bar” utility in shell script is very simple. Here is an example of a bash shell script:
echo "Starting untaring image..."
bar image.tar.bz2 | tar xjf -
echo "Untarring successfully completed"

The above example that we saw above is just a tip of the iceberg. I highly encourage you to read the man page for bar to explore it’s true potential:
# man bar

You can change the color of the foreground and background, change the labels, etc.

Additional Examples

Following are some examples straight from bar’s man page:

Example 1: Using bar to copy a 2.4gb file from a device (in this case a tape drive) to a file, using a 64k buffer.
# bar --in-file /dev/rmt/1cbn --out-file tape-restore.tar --size 2.4g --buffer-size 64k

Example  2: Using bar to copy a 37tb file across the network using SSH.

# ssh remote `dd if=file` | bar --size 37t > file

Example 3: Using bar inside a tar-pipe command:
Normal tar-pipe command might be:

# (cd /some/dir/somewhere && tar -cf - *) | (cd /some/other/dir && tar -xBpf -)

3a: Using bar within the tar-pipe:
# cd /some/dir/somewhere && tar -cf - *)  | bar |  (cd /some/other/dir && tar -xBpf -)
3b: Using bar with the –size option in a tar-pipe:
# du -sk /some/dir/somewhere
6281954 /some/dir/somewhere
# (cd /some/dir/somewhere && tar -cf - *)  | bar --size 6281954k |  (cd /some/other/dir && tar -xBpf -)

Example 4: Using bar on a regular file.  (Note that the  –size  option is not needed here, as bar will retrieve the file size itself.)
# bar  --in-file  ./file  |  ssh  remote ` cd  /some/dir && dd of=file`

Example 5: Generating a 512k file of random data

# dd if=/dev/random bs=1024 count=512 | bar -s 512k -of ./random

As you can see from the above examples, that bar is a very powerful and useful tool.

Enjoy Bar’ing….:)

As usual, please leave a comment/feedback if you have any. Comments encourages bloggers to post more and keep their spirits high.

Also don’t forget to rate this post below.

Be Sociable, Share!