Simple user interface for bash scripts

Command line scripts are very handy for many purposes. Recently we encountered a problem running a bash script on an embedded Linux target, which was equipped only with a touch screen. To run the scripts a few input arguments were necessary, but without a keyboard it was not feasible.
Rewriting the scripts with a GUI-aware language was not an option.

I then recalled using Zenity for a few things and thought it would be great time to take a closer look.

Zenity

Zenity is a cross-platform program for displaying simple dialog boxes and things that may be useful. The dialog boxes use the GTK+ Widgets. At the moment Zenity is available for Linux, BSD and Windows. It can be used from scripting languages such as Bash, Python (PyZenity), Perl, etc.
It can pop up alerts, progress bar windows, open/save dialogs, etc. Not all of the available dialog types are usable for touchscreen handling, but a few can be used.

Simple notification

For notifications such as errors, information and warnings Zenity offers some simple modal dialogs.

Here an example for bash script:

#!/bin/bash
zenity --warning --text="The system will shut down"

A less known feature of Zenity is text formatting (it cannot be found in the man-pages).

zenity --info --text=’this is <b>bold</b> and this is in <span color=”red”> color</span>’

Obviously the used syntax is similar to html tags.

zenity --error --text='<span size="xx-large">largest</span> and <span size="xx-small">smallest</span>'

Querying user input

For querying user input, Zenity offers some simple dialogs such as question, list, scale, text, password and file-selection. The last three, text, password and file-selection, require a keyboard and are out of focus for touchscreen support.

To get the input from the dialog you could use the following snippet.

#!/bin/bash
…
j=$(zenity --question --text="Are you sure you want to shutdown?"; echo $?)
echo "result:$j"

For ‘Yes’ you get a 0 and for ‘No’ a 1.

For the selection of a number in a range you could use scale:

#!/bin/bash
…
i=$(zenity --scale --text="Gauge" --value=20 --min-value=10 --max-value=110 --step=2)
echo $i

To select multiple options you could use list:

#!/bin/bash
…
k=(zenity --list --text="Selections" --checklist --column="Pick" --column="options" \
FALSE "Select1" \
TRUE  "Select2" \
FALSE "Select3" \
TRUE  "Select4" \
--separator=":")

echo “result: $k”

The output can be parsed. For convenience you can set the separator of the arguments with the ‘separator’ argument.

result: Select2:Select4

Displaying status

Zenity offers the possibility to display a progress indication and a text-info dialog.
These dialogs are of modal type. To display a continuous progress you can encapsulate the main flow in a function and pipe the output to Zenity:

#!/bin/bash
function test()
{
  i=0
  while [ $((i <= 100)) '=' 1 ]
    echo $i
    sleep 1
    i=$((i + 5))
  done
}

test | zenity --progress --auto-close

The text-info type can be used for a more verbose output. The principle to display a continuous progress can be implemented as mentioned above:

function createImage()
{
…
}

createImage| zenity --text-info

Conclusion

As you can see, Zenity makes it very easy to write shell scripts that interact with the user via the GUI. The user interface is not as refined as one that could be provided by a full-featured GUI application, but it is perfectly suitable for simple interactions. Some really useful dialog types are readily available for simple and basic user interaction on a touchscreen.

This entry was posted in UI, Uncategorized and tagged , , , . Bookmark the permalink.

5 Responses to Simple user interface for bash scripts

  1. Ikem says:

    In the example for the progress bar is the closing “}” missing.

  2. Marc Jansen says:

    Hi,

    I m working at the moment with the zenity command.
    The html functions like and working fine in some cases from zenity.
    I m using 2 scripts in zenity now.
    One with the zenity –Info and one with zenity –entry
    those html functions work for me fine with zenity info but seems not be recognizable in zenity entry

    script 1 works fine
    zenity –info –timeout=60 –width=400 –height=200 –display=:0.0 –text=”Beste computergebruiker.\n\nElk uur zal er een informatie venster openen,\nwaar je jouw
    voornaam (eventueel ook eerste letter van je achternaam)

    script 2 fails
    naam=””
    naam=$(zenity –entry –timeout=500 –width=800 –height=100 –display=:0.0 –title=”Informatie” timeout=500 –text=”Vul AUB uw voornaam en eventueel eerste letter van uw achternaam in:\nZo kunnen wij onze subsidies behouden \nvoor het vrij gebruik van de computers\nVriendelijk bedankt”)

  3. Marc Jansen says:

    not good , i posted this comments with command lines, it looks like it changed it here (executed the lines)
    np, i will search on

  4. I feel that is among the so much significant information for me.
    And i am satisfied studying your article. But want
    to remark on few common things, The site style is wonderful,
    the articles is really nice :D. Good process, cheers.

Leave a Reply

Your email address will not be published. Required fields are marked *