This is a read-only archive. Find the latest Linux articles, documentation, and answers at the new Linux.com!

Linux.com

Feature

CLI Magic: Creating basic front ends with dialog and Xdialog

By Shashank Sharma on July 17, 2006 (8:00:00 AM)

Share    Print    Comments   

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 syntax dialog [common option] [box-option] text width height . 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.

#!/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

Basic Menu 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

Gauge 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 text height width [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] height width as options. The $HOME/ argument means that we want to choose a file within the home directory.

Conclusion

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.

Shashank Sharma specializes in writing about free and open source software for new users and moderates the Linux.com forum boards. He is the coauthor of Beginning Fedora, published by Apress.

Share    Print    Comments   

Comments

on CLI Magic: Creating basic front ends with dialog and Xdialog

Note: Comments are owned by the poster. We are not responsible for their content.

afraid of the command line ...

Posted by: Anonymous Coward on July 17, 2006 04:41 PM
Er<nobr> <wbr></nobr>...,<nobr> <wbr></nobr>... you mean that new linux users afraid of the command line are supposed to write dialog scripts to overcome their fear<nobr> <wbr></nobr>.... ?

Funny

#

Re:afraid of the command line ...

Posted by: Administrator on July 17, 2006 09:53 PM
No, I mean, people like you and me can write basic frontends so that good command line tools are not left out just because they are command line.

#

temp file

Posted by: Anonymous Coward on July 17, 2006 09:13 PM
Script works fine with a minor adjustment

tempfile=`tempfile`

should be tempfile=`mktemp tempfile`

or can be tempfile=`mktemp<nobr> <wbr></nobr>/tmp/tempfile`


          and this should work fine.

#

Re:temp file needed fix

Posted by: Anonymous Coward on July 18, 2006 11:15 PM
Seems ubuntu does things different. FC4 needs the mktemp command.

#

works fine!

Posted by: Administrator on July 17, 2006 09:52 PM
on my ubuntu, tempfile='tempfile' wirks just fine.

#

--fselect: dialog vs Xdialog

Posted by: Administrator on March 13, 2007 05:21 AM
In the last example, you didn't include a sample graphic of how this screen should look - may I suggest you modify the article to show one?



I tried running the code for the example, but with Xdialog instead of dialog. The resulting screen was confusing, and obviously not what was described in the article.



As it turns out, for my system, at least, the size given for the box was too small, resulting in a box with no panes!



I fixed the problem by setting a larger size for the dialog box:



--fselect $HOME/ 28 68



After figuring this out, I also realized that I could have re-sized the original too-small box with the mouse... HTH.



Problem: with the character-based dialog version, I can't figure out how to make the file pane update when a directory is chosen from the direcory pane. The file pane automatically updates with the Xdialog version, but not the dialog version. Is there a way to make this happen with the character-based dialog version?

#

This story has been archived. Comments can no longer be posted.



 
Tableless layout Validate XHTML 1.0 Strict Validate CSS Powered by Xaraya