Appendix B. An introduction to dialog

dialog is a program that will allow you do draw text boxes on the screen in a prittier manner than a simple ascii-art drawing. It uses the ncurses library to achieve this. Before proceeding with this short one-page tutorial, ensure that you have dialog installed on you system. Refer to earlier in the notes if you cannot install some software on you machine, or alternately, do the System Administrators course.

Try this for instance:

dialog --infobox "Installing Windows 95, please wait..." 3 50

Dialog can take many different parameters. So, adding a background title is easy.

dialog \
            --backtitle "Welcome to Windows 95" \
            --infobox "Formatting your hard disk .... please wait" 3 80

Add this to the above line, and it might be worth putting it into a simple script. Let's call the script setup.exe (just for fun).

U=`echo $USER|tr '[a-z]' '[A-Z]'`; \
            OS=`echo $OSTYPE|tr '[a-z]' '[A-Z]' \
            `; dialog --sleep 5 --title "Welcome back $U, \
            we've you been?" \
            --backtitle "Windows 95 copying...." \
            --infobox "Erasing $OSTYPE" 3 40

As you can see, we're combining a number of options into a single dialog string. In this way, we can get it to do a number of things at once.

Enough tricks. Now, what about a menu? Simple enough:

dialog --menu "Hamish's simple menu system" 10 35 3 \
	"Option 1" "Slurp seafood" \
	"Option 2" "Quaff a steak" \
	"Option 3" "Sluk a salamander"

You will notice that the options are names. If I wanted them to be numbers, that would be as simple as:

dialog --menu "Hamish's simple menu system" 10 35 3 \
	"1" "Slurp seafood" \
	"2" "Quaff a steak" \
	"3" "Sluk a salamander"

Output from the dialog option goes to standard error, so catching that should be a matter of:

dialog --menu "Hamish's simple menu system" 10 35 3 \
	"1" "Slurp seafood" \
	"2" "Quaff a steak" \
	"3" "Sluk a salamander" 2> /tmp/option

Once we have the option, we can operate on it as we would have in any other shell script:


choice=`cat /tmp/option`

case $retval in
    echo "'$choice' chosen.";;
    echo "Cancel pressed.";;
    echo "ESC pressed.";;

There are a myriad of other things you can do with dialog. Consult the manual page for dialog(1), as well as the many examples in the docs directory where all documentation for available packages are stored on your system.