Testing Dialog in shell scripts

Testing Dialog in shell scripts

professional interface in shell applications

Dialog is an utility for creating professional-looking dialog boxes within shell scripts, this article just follows the introductory tutorial and present the examples of how and where it can be used.

Install Dialog

First, check whether you have installed it or not: which dialog. If not, just simply install it with brew install dialog, or sudo apt install dialog.

Dialog basics

Dialog allows you creating text-based color dialog boxes from any shell language, which supports 8 types of dialogs:

  • yes/no
  • menu
  • input
  • message
  • text
  • info
  • checklist
  • radiolist

It’s easy to use. To have a feeling on it, just type the following one-line command in Shell.

dialog --title "Message box" --msgbox 'Hello, world!' 5 20

simple message box

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 as specified, with message centred 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 optional 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.

yes/no box

The ‘yesno’ dialog box is very similar to our first message box example:

dialog --title "Message" --yesno "Are you having fun?" 6 25

yes/no box

In this example, you’ll see two buttons at the bottom, labelled as “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 Yes and 1 if 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 is too small, then characters will be lost.

info box

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 still going on:

dialog --infobox "Please wait..." 10 30 ; sleep 5

info box

input box

The input box allows the user to enter a string. The usual editing keys can be used, and the text fields scrolls if necessary. After the user inputs 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.txt

# cat answer.txt
# Frank Lin

input box

text box

The text box is a simple file viewer; it takes a filename as a parameter:

dialog --textbox /etc/profile 22 70

text box

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 then press the 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 menu-height.

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)

menu box

checklist box

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

checklist box

The third field in each choice is the initial state; -either on or off. Also, you can redirect the selection into a file, the selected tag(s) will be recorded.

radiolist box

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

radiolist box

Real application

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):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/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 mounted usb disk's name
  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

Advanced Features

You can create and use a dialogrc file to customise the color and appearance of the dialog boxes. More information are given in Dislog’s man page. For more examples of using Dialog you can look at the samples included in the Dialog source code.

THE END
Ads by Google

林宏

Frank Lin

Hey, there! This is Frank Lin (@flinhong), one of the 1.41 billion . This 'inDev. Journal' site holds the exploration of my quirky thoughts and random adventures through life. Hope you enjoy reading and perusing my posts.

YOU MAY ALSO LIKE

Using Liquid in Jekyll - Live with Demos

Web Notes

2016.08.20

Using Liquid in Jekyll - Live with Demos

Liquid is a simple template language that Jekyll uses to process pages for your site. With Liquid you can output complex contents without additional plugins.

Setup an IKEv2 server with strongSwan

Tutorials

2020.01.09

Setup an IKEv2 server with strongSwan

IKEv2, or Internet Key Exchange v2, is a protocol that allows for direct IPSec tunnelling between networks. It is developed by Microsoft and Cisco (primarily) for mobile users, and introduced as an updated version of IKEv1 in 2005. The IKEv2 MOBIKE (Mobility and Multihoming) protocol allows the client to main secure connection despite network switches, such as when leaving a WiFi area for a mobile data area. IKEv2 works on most platforms, and natively supported on some platforms (OS X 10.11+, iOS 9.1+, and Windows 10) with no additional applications necessary.

Hands on IBM Cloud Functions with CLI

Tools

2020.10.20

Hands on IBM Cloud Functions with CLI

IBM Cloud CLI allows complete management of the Cloud Functions system. You can use the Cloud Functions CLI plugin-in to manage your code snippets in actions, create triggers, and rules to enable your actions to respond to events, and bundle actions into packages.