Zenity create a progress dialog

Share this post to your friends !

This entry is part 7 of 13 in the series GUI for bash script using Zenity

WriteBash - In this article, we will talk about how to make progress dialog. We will use the --progress option in zenity to do this. You can easily see the software used to download or install have a progress dialog. So have you ever wondered how to create such a progress dialog?

Create a simple progress dialog

Once you have used zenity a few times, it’s easy to find the zenity option that supports creating a progress dialog. Type the following command to create it:

zenity --progress
zenity-create-a-progress-dialog-01 Zenity create a progress dialog
Create a progress dialog

But as you can see, the dialog is not customized as expected. It’s just a default dialog box and there are no features to allow you to manipulate it.

Zenity reads input data one by one. If the line starts with the # character, it will update the text content by the content of that line. If the line starts with a number, it will update the content by the content of that line. What does this mean? I will explain below.

Progress dialog options

This dialog supports the options presented in the table below.

Progress dialog options
OptionMeaning
--text=textThe text is displayed on the dialog. For example, you can look at the example in the image below. As i said above, zenity will replace the content of line beginning with # for the content of this option.
--percentage=percentagePercentage that is set in the dialog. As i said too, zenity will read data from input and replace content of the line begin with a number for the content of this option.
--auto-closeAutomatically close progress dialog when it reaches 100%.
--pulsateSpecifies that the progress bar pulsates until an EOF character is read from standard input. I rarely use this option, you can try it if you want.
zenity-create-a-progress-dialog-02 Zenity create a progress dialog
Create progress dialog with text option

Script to create a progress dialog

You copy the script below and assign the executable to the script.

Read moreCreate a file selection dialog

#!/bin/bash
# Script author: Danie Pham
# Script site: https://www.writebash.com
# Script date: 12-01-2018
# Script ver: 1.0
# Script use to create a progress dialog with zenity

# Function create a progress dialog
f_create_dialog () {
    # Create a list contain some commands
    (
        echo "10" ; sleep 1 # This command will echo a line begin with number "10". It will replace --percentage option.
        echo "# Updating mail logs" ; sleep 1 # This command will echo a line begin with # character. It will replace --text option.
        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 reads the data from the commands in the list above
    zenity  --progress \
            --title="Update System Logs" \
            --text="Scanning mail logs..." \
            --percentage=0

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

# Function main, use to call funtion f_create_dialog and other
f_main () {
    f_create_dialog
}
f_main

exit

Let’s try to use scripts to create a dialog and see what the results is.

chmod +x create_progress_dialog
./create_progress_dialog
zenity-create-a-progress-dialog-03 Zenity create a progress dialog
Script create progress dialog

Conclusion

In the script above, you can replace the echo commands in the list with your other functions or commands. You can adjust it to get your current system information and put the data into zenity to display. A fairly simple but effective command if you can take advantage of it with the bash script.

Continue reading the series«« Previous part: Zenity create file selection dialogNext part: Zenity create a form dialog »»
If you liked this article, then please subscribe to our YouTube Channel for more video tutorials. You can also find us on Twitter and Facebook.


Share this post to your friends !
If you appreciate what we share in this blog, you can support us by:
  1. Stay connected to: Facebook | Twitter | Google Plus | YouTube
  2. Subscribe email to recieve new posts from us: Sign up now.
  3. Start your own blog with SSD VPS - Free Let's Encrypt SSL ($2.5/month).
  4. Become a Supporter - Make a contribution via PayPal.
  5. Support us by purchasing Ribbon Lite Child theme being using on this website.

We are thankful for your support.

single post bottom banner
«« »»

Got something to say? Join the discussion

Please keep in mind that all comments are subject to our Comment Policy. Your email address will not be published.
This site uses Akismet to reduce spam. Learn how your comment data is processed.