Monday, December 7, 2009

Make your bash scripts user friendly using – dialog

 

If you have installed Linux using the text installer, then you will find a neat professional looking install process. You can rest assured that no extreme programming has gone into creating the text installer. In fact, it has been created using a utility called dialog. Dialog is a utility installed by default on all major Linux distributions. It is used to create professional looking dialog boxes from within shell scripts.

Some of the dialogs supported are Input boxes, Menu, checklist boxes, yes/no boxes, message boxes, radio list boxes and text boxes.

Creating a dialog is very easy. Here I will explain how to create dialog boxes of different types.

[Note: First of all check whether the dialog package has been installed in your machine or not.

Type: rpm –qa dialog*

If you find that the package is not installed then install it manually using commands,

depending on ur convenience and the type of your distribution.

rpm –ivh dialog* (incase you have dialog rpm file in your machine)

Or

yum install dialog*

Or

apt –get install dialog* ]

Input boxes: These allow the user to enter a string. After the user enters the data, it is written to standard error. You may also redirect the output to a file.

$ dialog --title "Ravi's Input Box"

--inputbox "Enter the parameters..." 8 40

As you can see, the options are self explanatory. The last two options 8 and 40 are the height and width of the box respectively.

clip_image001

Fig: Inputbox

Textbox: This is a box which takes a file as the parameter and shows the file in a scrollable box.

$ dialog --title "textbox" --textbox ./myfile.txt 22 70

... it shows the file myfile.txt in a textbox.

clip_image002

Fig: Textbox showing the file.

Checklist: The user is presented with a list of choices and can toggle each one on or off individually using the space bar.

$ dialog --checklist "Choose your favorite distribution:"

10 40 3

1 RedHat on

2 "Ubuntu Linux" off

3 Slackware off

... here, 10 is the height of the box, 40 - width, 3 is the number of choices, and the rest are the choices numbered 1,2 and 3.

Radio list: It displays a list containing radio buttons. And the user can only choose one option from the set of options.

$ dialog --backtitle "Processor Selection"

--radiolist "Select Processor type:"

10 40 4

1 Pentium off

2 Athlon on

3 Celeron off

4 Cyrix off

10 and 40 are the height and width respectively. 4 denotes the number of items in the list.

Infobox: This is useful for displaying a message while an operation is going on. For example, see the code below:

$ dialog --title "Memory Results"

--infobox "`echo ;vmstat;echo ;echo ;free`"

15 85

clip_image003

Fig: Information box - listing the vmstat and free listing.

clip_image004

Fig: Message box

Dialog is usually used inside a script which gives the script a degree of user friendliness. There is another package called Xdialog which gives the same features for scripts executed in X Windows. Xdialog utility also has additional functionality not found in the dialog utility.

To know more about the dialog utility check the man page of dialog.

 
Things You Should Know About Linux !!!