How to make a progress bar work in Zenity?

Solution 1:

The Zenity docs have a small code snippet that should do exactly what you're looking for.

#!/bin/sh
(
echo "10" ; sleep 1
echo "# Updating mail logs" ; sleep 1
echo "20" ; sleep 1
echo "# Resetting cron jobs" ; sleep 1
echo "50" ; sleep 1
echo "This line will just be ignored" ; sleep 1
echo "75" ; sleep 1
echo "# Rebooting system" ; sleep 1
echo "100" ; sleep 1
) |
zenity --progress \
  --title="Update System Logs" \
  --text="Scanning mail logs..." \
  --percentage=0

if [ "$?" = -1 ] ; then
        zenity --error \
          --text="Update canceled."
fi

First try just copying in the code that's there and running it and confirming that it works as intended, then modify to add in your code where appropriate.

If your progress bar is stuck at zero, make sure to by-pass any sections of the script that may be hanging and making you think that it's actually working!

Edit: As stated in the Answer below, the reason it's not working is because zenity expects the progress to be echoed to it, like in the code sample.

Solution 2:

The way zenity works for displaying progress bars is capturing your echo commands from your bash script via the | (pipe) redirection command (symbol).

Here is an example you can try that I lifted from Ubuntu Forums:

#!/bin/bash

# Force Zenity Status message box to always be on top.


(
# =================================================================
echo "# Running First Task." ; sleep 2
# Command for first task goes on this line.

# =================================================================
echo "25"
echo "# Running Second Task." ; sleep 2
# Command for second task goes on this line.

# =================================================================
echo "50"
echo "# Running Third Task." ; sleep 2
# Command for third task goes on this line.

# =================================================================
echo "75"
echo "# Running Fourth Task." ; sleep 2
# Command for fourth task goes on this line.


# =================================================================
echo "99"
echo "# Running Fifth Task." ; sleep 2
# Command for fifth task goes on this line.

# =================================================================
echo "# All finished." ; sleep 2
echo "100"


) |
zenity --progress \
  --title="Progress Status" \
  --text="First Task." \
  --percentage=0 \
  --auto-close \
  --auto-kill

(( $? != 0 )) && zenity --error --text="Error in zenity command."

exit 0

If you follow the link to Ubuntu Forums you can read a discussion of this script. If after that you still have questions please ask via comment below and I'll do my best to answer them for you.