How to make a progress bar work in Zenity?

12,184

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.

Share:
12,184

Related videos on Youtube

na-no.
Author by

na-no.

Updated on September 18, 2022

Comments

  • na-no.
    na-no. almost 2 years

    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.

    • sudodus
      sudodus almost 7 years
      Please edit your original question in order to show your bash script, if you need more detailed help than via the link to the zenity docs, (help.gnome.org/users/zenity/stable/progress.html.en).
    • WinEunuuchs2Unix
      WinEunuuchs2Unix almost 7 years
      You didn't say how "you tried to make it move from 0 to 100". More importantly like @sudodus requested post your code so we can critique it.
    • na-no.
      na-no. almost 7 years
      @WinEunuuchs2Unix I cannot upload the .sh script file right now. Maybe tomorrow. See you tomorrow, then!
    • na-no.
      na-no. almost 7 years
      @sudodus I am going to upload the script to Google Drive today, edit the post and type the link.
    • na-no.
      na-no. almost 7 years
      @WinEunuuchs2Unix I am going to upload the script to Google Drive today, edit the post and type the link.
    • na-no.
      na-no. almost 7 years
      @sudodus The script has uploaded. Check my post again to download and review it. Bajiru
    • na-no.
      na-no. almost 7 years
      @WinEunuuchs2Unix The script has uploaded. Check my post again to download and review it. Bajiru
    • sudodus
      sudodus almost 7 years
      I am running Lubuntu 16.04.1 LTS, up to date (with the xenial kernel and zenity version 3.18.1.1. The command zenity --progress --title='Analyzing data...' --pulsate pulsates for me, when started from a terminal window. After making your script executable (with chmod), I can run it and the last command line makes the pop up window pulsate too :-) Please describe what works, and what does not work for you.
    • WinEunuuchs2Unix
      WinEunuuchs2Unix almost 7 years
      I could not open your script from my phone. Google says File type not supported for both links. Why not copy script directly into your question?
    • WinEunuuchs2Unix
      WinEunuuchs2Unix almost 7 years
      @Bajiru I hope you don't mind but I removed the links to google drive, downloaded your script and placed it into the question directly to make it easier for others to review your situation.
    • na-no.
      na-no. almost 7 years
      @WinEunuuchs2Unix I don't mind, since you edited my post because the Google Drive links don't work. It's OK. :)
    • na-no.
      na-no. almost 7 years
      @sudodus A very big "Thank you!!" to all of you who helped me. Sorry for the script upload being so delayed, but I was out of home for some days. Thanks to sudodus with his html about Zenity. (comment continues below)
    • na-no.
      na-no. almost 7 years
      @WinEunuuchs2Unix Also, thanks to WinEunuuchs2Unix for dealing with and helping with my post and script. (comment continues below)
    • na-no.
      na-no. almost 7 years
      @Bujiraso And I could not forget Bujiraso with his code snippet from the Zenity docs which really helped me! Thank you, all of you! You helped me as much as you could.
    • na-no.
      na-no. almost 7 years
      I "cut" my comment in three parts because I wanted to notify all of you on one comment, but I couldn't. Again, thank you!
    • WinEunuuchs2Unix
      WinEunuuchs2Unix almost 7 years
      @Bajiru You're welcome. Glad things worked out :)
    • na-no.
      na-no. almost 7 years
      @WinEunuuchs2Unix Yes!
  • WinEunuuchs2Unix
    WinEunuuchs2Unix almost 7 years
    This is more a comment than an answer but perhaps you don't have sufficient rep yet to post comments. So I'll upvote to give you 10 more points-- not because this is a good answer, but because it's a good comment.
  • that other guy
    that other guy over 6 years
    I get a 403 Forbidden on this page :/
  • Bujiraso
    Bujiraso over 6 years
    You're right. Fixed, I think.