How to make simple graphical shell scripts in Linux with Zenity [Tip]

Zenity is a feature in Linux that adds graphical interfaces to the shell scripts with a single command. Shell scripts are robust and effective way to mechanize monotonous tasks, but they are usually limited to the terminal. While Zenity transports them directly out of terminal to your computers desktop. If you know the basics of shell scripting. You must know that you do not have to be a programmer start using and applying shell scripts. On other hand shell scripts needs more than mere information of the Linux terminal commands.zenity

Getting Zenity

If using Ubuntu Zenity comes with it by default. But if you use some Ubuntu derived, like Kubuntu you can have it installed manually with the following command:

sudo apt-get install zenity

Basically, Zenity is a fragment of GNOME, so must already be contained within the Linux distributions which uses the GNOME desktop. Just check the package manager of zenity.

Using Zenityzenity2

You can use Zenity from within the terminal. Let say you need to make some error window when a problem happens with the shell script. Here is the command you can make use of.Type zenity –error<space> –title=message you want to display –text=

Run above-mentioned command and you will see a window with an error message.

If you place this lone command in the shell script at a certain place then you will get a graphical error note. You can use different variables to add more info about the fault.

Let us say that you want to inquire a simple yes or no question. Then you can employ command like this.Type

zenity –question –title=Query –text=

Would you like to run the script?

Then you can simply catch yes or no answer within your shell script and accomplish different commands based on which button user clicks.

You can also use a text entry dialog by simply typing the command.just type zenity<space>–entry<space>–title=sitesbadress–text=

What is your favorite website?

Get the input of the user in the shell script and you can pile it as variable. There are many other options you can make use of like

  • calendar
  • file picker

 

And many other types. You can get a full list of dialog types available with their alternatives from visiting Zenity’s manual page.

An Example Script

Let us attempt using Zenity to make some simple and easy graphical shell script. We will use only three commands here. To make a graphical timer. Type the following command

#!/bin/bash

Enter a duration for the timer.\n\n Use 5s for 5 seconds, 10m for 10 minutes, or 2h for 2 hours.)

sleep $TIME

zenity –info –title=Timer Complete –text=

The timer is over.\n\n It has been $TIME.

We are starting to peel the cover of a big apple here. Zenity is a large topic. But we have tried to show you how you can use zenity for different purposes. Zenity can also be used to create more complicated programs. If you are interested more about the information on shell scripting there are plenty of books and data on and off the internet you can make use of.

Related Posts