Dialog creates the simple front ends that until recently were used in almost all Linux distributions' installations. Remember old Red Hat and Slackware installations that were characterised by a grey screen with a blue background? Since these front ends are text-based, you can't use a mouse to click on buttons.
Xdialog, on the other hand, creates X interfaces, meaning that you have full use of your mouse. It uses GTK+ for creating the front ends, and offers functions such as directory selectors and range boxes.
Once you have installed dialog, open a shell and write
dialog --title "Testing Dialog" --yesno "This is the message" 8 25, then press Enter. This creates a very basic yesno box. You specify box options using the general syntax
dialog [common option] [box-option] textwidthheight. So, the 8 and 25 above are the width and height.
--title is a common option. Common options are applied to all the box options. The common options can be omitted, you you cannot make a box without specifying the box options.
To test Xdialog, just change
dialog in the above command to
Both dialog and Xdialog can implement several types of dialog boxes, such as checklist, form, radiolist, menu, and textbox. Each dialog box has its own set of box options. Let's create a simple menu to better understand the [common] and [box] options.
Creating a menu
When making a menu, you may be overwhelmed by all the available choices. I am going to start with a simple menu that lets you select one value out of several.
#!/bin/sh tempfile=`tempfile` dialog --title "Most used linux command" \ --menu "Please choose the most usefull command line tool:" 15 55 5 \ "man" "To read man pages" \ "ls" "To display the contents of a directory" \ "vi" "Text editor" \ "mount" "To mount partitions" \ "su" "Super user permissions" 2> $tempfile return_value=$? you_chose=`cat $tempfile` case $return_value in 0) echo "'$you_chose' is the command you find most usefull.";; 1) echo "You pressed cancel.";; 255) echo "You hit Esc.";; esac
Save this code in a file called
basic_menu.sh. To make this script executable, run
chmod u+x basic_menu.sh. Now you can run the script:
This creates a very basic menu, where you can choose any command using the up and down keys. The most noteworthy lines of this script are
tempfile ='tempfile' and
2> $tempfile. The first line creates a temporary file using the tempfile utility. Dialog, by default, writes its output to standard error, so we need the second line to redirect the output from standard error to the tempfile. We can also use the
--stdout option to send the output to standard output. We'll look at
--stdout in a moment.
$? is a variable that stores the program's return value, which is either 0, when you press OK; 1, if you press cancel; or 255, if you press Esc.
return_value is another variable where we store the value of the $? variable.
To see how this dialog would appear in X, change dialog to Xdialog. Everything else remains the same.
You never see this type of menu in the real world. You see either a radiolist, where you can select just one value, or a checklist, which allows you to select multiple values. To create a radiolist, replace
--radiolist, keeping rest of that line intact. You also need to change some options as below:
"man" "To read man pages" off\ "ls" "To display the contents of a directory" off\ "vi" "Text editor" off\ "mount" "To mount partitions" off\ "su" "Super user permissions" ON 2> $tempfile
See how each of the options has either "off" or "on" at the end? "On" signifies a selected option. To select any option, move to your choice using the up and down keys and press the spacebar.
Unlike a radiolist, a checklist allows you to select multiple values. If you change
--checklist, you get a checklist.
Most of us are so accustomed to working in graphical environments that we cannot imagine some of the dialogs that you can create on the command line. For example, you can easily make a progress bar or percentage gauge bar, which is part of every software installation, with dialog and Xdialog. The syntax is
--gauge textheightwidth [percent]. The percent value shows the initial value of the progress bar. By default it is zero.
Let's create a shell script to see how the gauge works.
#!/bin/sh percent=0 ( while test $percent != 110 do echo $percent echo "XXX" echo "This is how the gauge appears" echo "XXX" echo "See how the message changes" echo "XXX" percent=`expr $percent + 10` sleep 1 done ) | dialog --title "Gauge works!" --gauge "This is how a gauge appears on the command line" 10 60 0
As explained in the man page, if the standard input reads the string "XXX," then subsequent lines up to another "XXX" are used for a new prompt. If you use the "XXX" method of displaying messages, then the text supplied to
--gauge is ignored. In our case, the message This is how a gauge appears on the command line is ignored because of the messages in "XXX."
Another common dialog is file selection. When attaching a file to an email message or opening a file in OpenOffice.org Writer, we frequently encounter the select file dialog. You can create that on a command line too. We are going to use
--stdout for this.
#!/bin/sh selection=`dialog --stdout --title "Use this dialog to select a file by pressing spacebar" --fselect $HOME/ 10 68` case $? in 0) echo "You chose \"$selection\"";; 1) echo "You pressed cancel.";; 255) echo "You hit Esc.";; esac
The file selection dialog is made up of two panes; you can use the tab key to switch between them. Select a file by pressing the spacebar once you have scrolled to the file name using the up and down keys. You can also write the path of the file in the input box at the bottom of the file selection dialog. The
--fselect option creates the file selection dialog. It accepts
[filepath] heightwidth as options. The $HOME/ argument means that we want to choose a file within the home directory.
Both dialog and Xdialog are complex tools that can create many types of dialog boxes. They allow a high degree of control on all aspects of the dialogs. The detailed man pages discuss all the features of these tools, and are a must read.
With a little shell scripting knowledge you can start making front ends to all your favourite command line tools.
Shashank Sharma is studying for a degree in computer science. He specializes in writing about free and open source software for new users.