4

I am writing a simple basic bash script (starting with #! /bin/bash, file format is .sh) and I am trying to make a progress bar work:

#!/bin/bash  
echo "You are running in LXDE mode. Answer 'yes' or 'no' on the following question to continue (or not) in LXDE mode."
zenity --question --text='Do you want to continue in LXDE mode?' --ok-label=Yes --cancel-label=No
echo "Please enter your username and password to continue because the following command needs root privileges."
zenity --password --username
echo "Please enter today's date:"
zenity --calendar --text="Please enter today's date:"
echo "Please enter your name:"
zenity --entry --text='Please enter your name on the text entry below:'
echo "Analyzing data..."
zenity --info --text='Now begin analyzing data. If it takes more than 40 seconds, click on "Cancel".'
zenity --progress --title='Analyzing data...' --pulsate

I have tried to make it move from 0% to 100%, and nothing happened. It was stuck at 0%. I have also tried to make it pulsate by using the --pulsate option, still at 0% doing nothing.

Can anyone please help me? Any help would be appreciated.

muru
  • 207,228
na-no.
  • 856
  • 1
  • 10
  • 24

2 Answers2

13

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.


Edit2: There was a suggested edit to change the test at the bottom to the following:

# Check return code and display an error dialog if the code is not success.
if [ "$?" != 0 ] ; then
        zenity --error \
          --text="Update canceled."
fi

This is good code, but I'm leaving the example as the snippet from the official docs page. This is not my code.

3

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.