Tuesday Tooling - Create GUIs for BASH Scripts

This week a handy GUI creation tool for your BASH scripts

So what is it?

Zenity is a tool that wraps BASH scripts and terminal commands in a lovely graphical user interface. So we have input dialogs, information / warning dialogs, calendars, file open / save dialogs..etc etc.

Why do I need a GUI for my shell?

Well you don't really, hardocde sysadmins will use the terminal as is, but if we are looking to create a quick tool for users to interact with, then a carefully crafted shell script may not appeal to them, rather a simpler interface appeals.

So how can I install it?

On most systems it is installed as standard, but if not then it can be installed quite easily from the terminal.

Debian / Ubuntu / Raspbian systems

sudo apt install zenity  

So how can I use it?

alt We can run zenity from the terminal, so to test it, lets open a terminal window and type the following.

zenity --info --text "Visit bigl.es"  

Press enter and you will be reminded to visit this website...

So lets put it to work with two BASH scripts.

Our first is a simple, capture input from the user, save the input as a variable, compare the variable against a required value, and if they match, say hello, else we do not say hello to the user.

Here is the code
Note that lines 2 and 3 are actually one really long line!

#!/bin/bash
Answer=$(zenity --entry --text "Who are you?" --entry-text "Tell me your name"); echo $Answer  
if [ $Answer == "Les" ]; then  
   zenity --info --text "Hi $Answer"
else  
   #echo "Who are you?"
   zenity --error --text "Who are you?"
fi  

And when in use it looks like this.
alt

Something a little more useful?

So lets build a tool that use radio buttons to enable a user to select an application from the list. This takes the value selected, saves it as a variable and then compares it against a series of values. If they match then the chosen application is launched. Oh and the & is Unix shorthand and means to run the command in the background, releasing the terminal back to the user.

Here is the code, you'll notice that "Chrome" is TRUE and the others are FALSE. This means that Chrome is pre-selected, handy to indicate a default app.

Note that lines 2 and 3 are actually one really long line!

#!/bin/bash
ans=$(zenity  --list  --text "Choose an application" --radiolist  --column "Pick" --column "Application" TRUE "Chrome" FALSE "Inkscape" FALSE "GIMP");  
echo $ans  
if [ $ans == "Chrome" ]; then  
   google-chrome &
elif [ $ans == "Inkscape" ]; then  
   inkscape &
elif [ $ans == "GIMP" ]; then  
   gimp &
else  
   zenity --error --text "Unknown Application "
fi  

And in use it looks like this.
alt

Why should you use it?

If you need to create a quick and simple GUI for an existing project.
Create an introduction to GUIs and there uses in class.
Look at cat pictures on reddit.

Zenity is an old tool, I remember creating an Ubuntu ISO "creation station" using Zenity, an O2 Joggler (Kids ask your parents) and an external CD writer in 2010 for Barcamp Blackpool. Them were the days!

Give it a whirl, see if it meets your needs.