Bash console progress dialog with command output

bashdialog

I have dialog installed, where I would like to have a nice progress dialog, like this:

+-------[ title ]--------+
|                        |
| +-[ console output ]-+ |
| | output line11      ^ |
| | output line12      | |
| | output line13      | |
| | output line14      # |
| | output line15      | |
| | output line16      v |
| +--------------------+ |
| #########80%#####::::: |
+------------------------+

So, for example when upgrading 5 packages in a distro, it shows the progress (here, 4 package is upgraded out of 5 ie. 80%), but shows the detailed output of the executed commands. Is this possible?

I suspect it is, but I cannot get a working solution with --tailboxbg and --gauge.

Best Answer

Right, you can do it with --gauge:

#!/bin/bash
declare PACKAGES=("/etc/crontab"  "/etc/dmtab"  "/etc/fstab"  "/etc/inittab"  "/etc/mtab")
NUM_PACKAGES=${#PACKAGES[*]} # no. of packages to update (#packages in the array $PACKAGES)
step=$((100/$NUM_PACKAGES))  # progress bar step
cur_file_idx=0
counter=0
DEST=${HOME}
(
# infinite while loop
while :
do
    cat <<EOF
XXX
$counter
$counter% upgraded

$COMMAND
XXX
EOF
    COMMAND="cp ${PACKAGES[$cur_file_idx]} $DEST &>/dev/null" # sets/updates command to exec.
    [[ $NUM_PACKAGES -lt $cur_file_idx ]] && $COMMAND # executes command

    (( cur_file_idx+=1 )) # increase counter
    (( counter+=step ))
    [ $counter -gt 100 ] && break  # break when reach the 100% (or greater
                                   # since Bash only does integer arithmetic)
    sleep 10 # delay it a specified amount of time i.e. 1 sec
done
) |
dialog --title "File upgrade" --gauge "Please wait..." 10 70 0

This code in action:

            progress bar animation

Note. This code actually copies those five files from /etc/ into your $HOME folder.

Related Question