Dialog is an utility for creating professional-looking dialog boxes within shell scripts, this article just follow the introductory tutorial and present the examples of how and where it can be used.
First, check whether you have installed it or not:
which dialog. If not, just simply install it with
brew install dialog.
Dialog allows you creating text-based color dialog boxes from any shell language, which supports eight types of dialogs:
Dialog is easy to use. To have a feeling on it, just type the following one-line example of a message box on
dialog --title "Message box" --msgbox 'Hello, world!' 5 20
This example creates a message box with the title of “Message box”, containing the greeting “Hello world!”. This box is 5 lines high and 20 characters wide, with message centered in the box. An “OK” button sits at the bottom, you can press Enter to dismiss the message box.
Dialog box types
Most calls to dialog are in a similar format: an optinal title, the dialog type, the text to be displayed, and the height and width of the dialog box. Additional parameters specific to the type you choose. Let’s have a brief look at each of the eight types.
The ‘yesno’ dialog box is very similar to our first message box example:
dialog --title "Message" --yesno "Are you having fun?" 6 25
Trying this example, you’ll see two buttons at the bottom, labeled “Yes” and “No”. You can select between the buttons using the cursors keys (or Tab) and make selection by pressing Enter. The exit status returned to the shell will be
0 if you chose
1 if a
No is selected. You can check the exit status with
echo $? after selection been made.
If the width is less than the string length, the string is wrapped around at word boundaries. If the dialog box too small, then characters will be lost.
The info box is similar to the message box except that it does not wait for the user to select the “OK” button. This is useful for displaying messages while an operation is going on:
dialog --infobox "Please wait..." 10 30 ; sleep 5
The input box allows a user to enter a string. The usual editing keys can be used, and the text fields scrolls if necessary. After the user enters the data, it is written to the
standard error, or more commonly you can redirect to a file as in the example:
dialog --inputbox "Enter your name:" 8 40 2>answer # cat answer # Frank Lin
The text box is a simple file viewer; it takes a filename as a parameter:
dialog --textbox /etc/profile 22 70
You can use movement keys in the text box: the cursor keys, Page Up, Page Down, Home, etc. Exit the text box by pressing Esc or Enter.
The menu type of box allows creating a menu of choices from which user can choose. Each menu entry consists of a “tag” string and an associated “item” string, both of which are displayed. The user can make a choice using the cursor keys and pressing Enter. The selected tag is written to the
standard error. In the following example, after specifying the height and width of the menu, the number
3 indicates the items of choices that specify the
dialog --menu "Choose one:" 10 30 3 'a)' red 'b)' green 'c)' blue # or # dialog --menu "Choose one:" 10 30 3 'a)' red 'b)' green 'c)' blue 2>selection # cat selection # c)
For the checklist box, the user is presented with a list of choices and can toggle each one on or off individually using the space bar:
dialog --checklist "Choose toppings:" 10 40 3 \ 1 Cheese on \ 2 "Tomato Sauce" on \ 3 Anchovies off
The third field in each choice is the initial state; -either
off. Also, you can redirect the selection into a file, the selected tag(s) will be recorded.
The radiolist box is essentially the same as the checklist except that the user must make one choice from the list of mutually exclusive options.
dialog --backtitle "CPU selection" \ --radiolist "Select CPU type:" 10 40 4 \ 1 386SX off \ 2 386DX on \ 3 486SX off \ 4 486DX off
The presented dialog boxes are normally used within the shell script to do some real works. Here is a simple but useful application from the tutorial article to back up Documents directory to an usb disk (with little modification):
#!/usr/local/bin/bash # Backup all files under Documents directory to a usb disk # Display message with option to cancel dialog --title "Backup" --msgbox "Time for backup of home directory. \ Insert formatted usb disk \ 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 ~/Documents # Backup using zip; redirect any errors to a temporary file # /Volumes/FAST is my usb disk mounted on OSX sudo zip -r /Volumes/FAST/backup.zip . >|/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
There are several more things that dialog can do. You can create and use a dialogrc file to customise the color and appearance of the dialog boxes. More information are given in the man page. For more examples of using dialog you can look at the samples included in the dialog source code.