In this tutorial, you'll learn how to show progress bar graphical window while processing some data using shell script in Linux/Unix systems.
Here I am giving two shell script examples; in the first example, it will only display a progress bar window using the dialog command and will loop through 1 to 100. In the second example, it will perform the file copy operation and will display the progress using the progress bar.
If the dialog is not installed on your Linux system, then check the following example to install on Ubuntu Linux systems:
sudo apt-get install dialog
Example 1. Show Progress Bar Window in Linux
#!/bin/bash
declare -i COUNTER=1
{
while test $COUNTER -le 100
do
echo $COUNTER
COUNTER=COUNTER+1
# do some task here
done
} | dialog --gauge "This is a progress bar" 10 50 0
Output
Example 2. Copy Files of Specified Criteria and Display Progress
#!/bin/bash
declare -i COUNTER=1
# Get total number of sh files into a variable
# Change the file type below
nfiles=`ls *.sh -f | wc -l`
# Get approx value to increment until 100
if [ $nfiles -gt 99 ]
then
ivar=1
else
ivar=`expr 100 / $nfiles`
fi
{
for filename in *.sh
do
echo $COUNTER
COUNTER=COUNTER+ivar
cp $filename $filename.bak
if [ $COUNTER -ge 100 ]
then
# reset to 1 if greater than 100
COUNTER=1
fi
done
# Dispay 100 if less than 100
COUNTER=100
echo $COUNTER
} | dialog --gauge "Please wait while backup in progress..." 10 50 0
exit 0
Output
See also:
- Linux: 1 to 15 Number Puzzle Game Using Shell Script
- Linux – Check Disk Space in MB (Megabyte) GB (Gigabyte) Form