July 17, 2006

CLI Magic: Creating basic front ends with dialog and Xdialog

Author: Shashank Sharma

New Linux users are often afraid of the command line. They prefer graphical alternatives to commands and scripts. For help, they can turn to dialog and Xdialog, two simple tools that can be used to create front ends to command-line tools.

The fundamental difference between the tools is that while dialog can create console or text-based front ends, Xdialog can generate an X interface for any terminal-based program.

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 syntaxdialog [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 Xdialog.

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.



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


you_chose=`cat $tempfile`

case $return_value in
    echo "'$you_chose' is the command you find most usefull.";;
    echo "You pressed cancel.";;
    echo "You hit Esc.";;

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: ./basic_menu.sh.

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 --menu with --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 --radiolist to --checklist, you get a checklist.

There's more

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.


while test $percent != 110
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
) |
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.


selection=`dialog --stdout --title "Use this dialog to select a file by pressing spacebar" --fselect $HOME/ 10 68`

case $? in
    echo "You chose \"$selection\"";;
    echo "You pressed cancel.";;
    echo "You hit Esc.";;

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.

Click Here!