Zenity create a progress dialog

by Daniel Pham
Published: Updated:
This entry is part 7 of 13 in the series GUI for bash script using Zenity

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
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.

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
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://devopslite.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
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.

(This is an article from my old blog that has been inactive for a long time, I don’t want to throw it away so I will keep it and hope it helps someone).

0 0 votes
Article Rating
Series Navigation«« Previous part: Zenity create file selection dialogNext part: Zenity create a form dialog »»

You may also like

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

DevOps Lite is a personal blog specializing in technology with main topics about DevOps, DevSecOps, SRE and System Administrator. Articles are shared for free and contributed to the community.

SUPPORT US

FOLLOW US

Subscribe my Newsletter for new blog posts. Stay updated from your inbox!

© 2021-2024 DevOpsLite.com – All rights reserved.

Please write sources “DevOpsLite.com” when using articles from this website.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

0
Would love your thoughts, please comment.x
()
x

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.