Oracle – for when it was like that when you got there

Main menu

Post navigation

Zen and the Art of Shell Scripting

You know what it’s like with a little kid at his birthday party. They get all excited, eat far too many sugary foods and then run around behaving badly.
Oracle Openworld has ended for another year, and not a minute too soon. Hopefully, Larry will have an early night and stop being so excitable and upsetting all those jolly nice open source types he had round.
Meanwhile, in the comparative calm of the backwater that is this blog, I’ve been getting all Zen. Well, zenity, to be precise. Yep, I’ve decided that some my batch scripts needed to get all GUI with Gnome and zenity looks to be the tool to do it.
What follows is an account of my first steps with zenity followed by a demonstration of some of it’s capabilities.

I was going to say “the first time I used zenity in anger”…but that wouldn’t really fit in with my attempts to attain enlightenment…so…the first problem I managed to solve with zenity arose from my adventures with VLC, which I posted here.

As you may recall, the solution I decided upon for getting VLC to read music from my Samba share was to mount it on my PC. Now, the command to do this requires sudo, and sudo, by default, asks for re-verification of my password.
I need to mount the share everytime I start VLC because that’s where all the music files are stored, but I don’t really want to have to open a terminal and type out the command ( or even run a batch script). So, how could I run VLC and mount the share from the menu without removing sudo’s secure habit of asking for a password ?
Well, first off, sudo can take the password from stdin by means of the -S switch. So, all we need to do is to provide a dialog box for the user to enter the password and then pipe it into sudo…

Now all I need to do is change the menu entry for VLC to run this script.
To do this, go into the Gnome System Menu, Preferences, Main Menu. In the Sound & Video section, select VLC and click Properties. In the Command box, just enter the full path to the script.
There is rather more to zenity than facilitating my musical indulgences. It offers a wide range of GUI interfaces that can be invoked from a shell script.
A full description of them can be found in the Zenity Manual.
There’s also a really good illustration of the available dialogs here.

I’ve taken this on just a little, to work out how to handle the user input coming back from these dialogs. I thought the best way to do it would be a quiz. A Karma Quiz.
What we’re going to do here is :

I’ve tried to make some of the messages sound suitably gnomic. Unfortunately, haikus are not really my strong point. Dirty limericks are more my metier.
The quiz consists of five questions of varying levels of obscurity. Question 3 ( about fruit salad) is taken from a Christmas cracker and is still one of my Mum’s favourite jokes. Well, she does tell it at every opportunity.

Before we get to the script, we need to create a text file for the scoring scale.
The file is called karma.txt and contains the following lines :

0-1 - Megalomaniac head of a Software Behemoth
2-4 - On the road to the (Leyton) Orient
5 - You have attained fortune-cookie wisdom