Dialog: An Introductory Tutorial
The preceding examples were somewhat unrealistic; dialog is normally used within a shell script to do some real work. Let's look at a simple but useful application. I use the following script to back up my home directory to floppy disk on a regular basis:
#!/bin/sh
# Backup all files under home directory to a single # floppy
# Display message with option to cancel
dialog --title "Backup" --msgbox "Time for backup \ of home directory. \
Insert formatted 3-1/2\" floppy and press <Enter> \ to start backup or \
<Esc> to cancel." 10 50
# Return status of non-zero indicates cancel
if [ "$?" != "0" ]
then
dialog --title "Backup" --msgbox "Backup was \ canceled at your
request." 10 50
else
dialog --title "Backup" --infobox "Backup in \ process..." 10 50
cd ~
# Backup using tar; redirect any errors to a
# temporary file
# For multi-disk support, you can use the
# -M option to tar
tar -czf /dev/fd1 . >|/tmp/ERRORS$$ 2>&1
# zero status indicates backup was successful
if [ "$?" = "0" ]
then
dialog --title "Backup" --msgbox "Backup \
completed successfully." 10 50
# Mark script with current date and time
touch ~/.backup
else
# Backup failed, display error log
dialog --title "Backup" --msgbox "Backup failed \ -- Press
<Enter>
to see error log." 10 50
dialog --title "Error Log" --textbox /tmp/ERRORS$$ 22 72
fi
fi
rm -f /tmp/ERRORS$$
clear
To run this automatically, I put these lines in my .profile file to call the backup script on login if more than 3 days has elapsed since the last backup was made:
# do a backup if enough time has elapsed find ~/.backup -mtime +3 -exec ~/.backup \;
The sound driver for the Linux kernel uses a program called “configure” to prompt the user for sound configuration options. It generates a C header file based on the chosen options. A replacement based on dialog could offer some advantages, such as a more professional appearance and the ability to select options randomly from menus rather than as a linear sequence of questions.
Due to time and space constraints, I only present a partial (but functional) implementation of a sound driver configuration script. This could quite easily be extended to fully replace the current configure program.
The complete script is shown in as Listing 1. I'd like to explain it using a top down approach, which means reading the listing starting from the bottom.
The last part of the script is a while loop which simply calls the shell function main_menu repeatedly. Above that is the code to implement the main menu. We present the user with three choices, and redirect the selection to a file. One of three shell functions is then called, based on the user's choice.
The most important menu in this script is the next one, the config_menu function. Again we present the user with a number of choices. Note that in this case there is an option which returns the user back to the main menu.
Continuing to read our listing backwards, we come to the select_cards function. The kernel supports multiple sound cards, so here we use a checklist to present the user with the available choices. The command “on_off” is a utility function that will be shown later; it returns the string “on” if its parameters are equal, otherwise it returns “off”. This is the form that the checklist menu requires. Note that the return status of the command is checked. If the user selects “cancel” from the menu then the return status is non-zero and we return immediately without making any changes. Otherwise, we set appropriate variables to indicate which sound cards have been enabled.
The next function, as we read our listing backwards, it the function view_summary. This uses the textbox type to display a file containing information on the currently selected options. We first build up the data in the file before displaying it.
Our next function is select_dma. Here the user must make one of four mutually exclusive options, so we use the a radio list. If you try this example yourself, be aware that the radiolist type was added in dialog version 0.4; if you have an older version then you will have to make do with a checklist.
Up above, the routine select_irq uses very similar code to allow the user to select the final option in our configuration utility.
The purpose of this script is to generate a C language header file defining the compile options for the kernel sound driver. The “save” function does this. Notice how a dialog box is displayed while the save is in progress.
Above that we see the on_off function alluded to previously. This avoids some repetitive code in the script.
Finally, we see the clean_up routine which allows the user to exit from the script. At the top of the script some default values are defined for the configuration options and the temporary filename to use.
The configuration utility still needs a few enhancements to replace the existing program, including more kernel options and error checking, but the example does function and gives a feel for what can be done with dialog. I encourage you to type it in and try it.
Today’s modular x86 servers are compute-centric, designed as a least common denominator to support a wide range of IT workloads. Those generic, virtualized IT workloads have much different resource optimization requirements than hyperscale and cloud applications. They have resulted in a “one size fits all” enterprise IT architecture that is not optimized for a specific set of IT workloads, and especially not emerging hyperscale workloads, such as web applications, big data, and object storage. In this report, you will learn how shifting the focus from traditional compute-centric IT architectures to an innovative disaggregated fabric-based architecture can optimize and scale your data center.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
| Non-Linux FOSS: Seashore | May 10, 2013 |
| Trying to Tame the Tablet | May 08, 2013 |
- Using Salt Stack and Vagrant for Drupal Development
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- New Products
- Validate an E-Mail Address with PHP, the Right Way
- Drupal Is a Framework: Why Everyone Needs to Understand This
- A Topic for Discussion - Open Source Feature-Richness?
- Home, My Backup Data Center
- New Products
- RSS Feeds
- Tech Tip: Really Simple HTTP Server with Python
- I like your topic on android
38 min 12 sec ago - Reply to comment | Linux Journal
59 min 22 sec ago - This is the easiest tutorial
7 hours 13 min ago - Ahh, the Koolaid.
12 hours 52 min ago - git-annex assistant
18 hours 52 min ago - direct cable connection
19 hours 14 min ago - Agreed on AirDroid. With my
19 hours 24 min ago - I just learned this
19 hours 28 min ago - enterprise
19 hours 59 min ago - not living upto the mobile revolution
22 hours 50 min ago
Enter to Win an Adafruit Prototyping Pi Plate Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Prototyping Pi Plate Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- Next winner announced on 5-21-13!
Free Webinar: Linux Backup and Recovery
Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.
In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.




Comments
Dialog Not displaying correctly
Hi Guys,
I'm writing an application using dialog for a production machine that does not have any window managers or desktops installed. My development environment has a gnome desktop and the terminal window seems to display dialogs correctly, but when I copy the script to the production environment, the display is misaligned and does not look as neat as what I see in the development environment.
I've tested the application on the production machine in a telnet session from my development machine, and then it displays correctly, so I can only assume that something is wrong with the terminal settings on the actual production environment.
Both operating systems are exactly the same. Do I need to change any terminal settings or configurations to make the dialogs display correctly?
Thanks
using dialog in Red hat Linux or SUSE linux
How do i use the dialog facility on Red Hat Linux and/or SUSe Linux. can you give a link where one can download the package
Standard Package Manger
You should be able to find them in the standard repositories using the standard package managers. On SuSE use zypper (or yast). On Red Hat use yum. From a shell prompt do:
Mitch Frazier is an Associate Editor for Linux Journal.
corrections
The lines:
dialog --title "Message" --yesno "Are you having\ fun?" 6 25
dialog --menu "Choose one:" 10 30 3 1 red 2 green\ 3 blue
should read:
dialog --title "Message" --yesno "Are you having fun?" 6 25
dialog --menu "Choose one:" 10 30 3 1 red 2 green 3 blue
the backslash is only needed to escape a new-line character for commands written over multiple shell command-lines. There are other occurences of this type of error in the article. This may have occured because of the web content software used on this site causing lines to be concatenated.
Check out the `Xdialog` package (http://xdialog.dyns.net/), which is a drop-in replacement for `dialog`, but does the same thing for an X Window GUI environment. Also, newer versions of `dialog` are sometimes referred to as `cdialog` (http://freshmeat.net/projects/cdialog/).
HTH.