Creating Interactive Scripts in Linux: Using Dialog or Whiptail

In the world of system administration, it is common to encounter the need to automate tasks through shell scripts. However, sometimes we need to make these scripts interactive to facilitate the user experience, especially in environments where a full graphical interface is not available. This is where tools like Dialog and Whiptail come into play.

Both Dialog and Whiptail are tools that allow creating simple and functional graphical interfaces within a text terminal. These tools are very useful for developing menus, dialog boxes, selection lists, progress bars, and much more. Throughout this article, we will guide you through the basic concepts and practical examples of both tools so that you can use them in your own scripts.

What is Dialog?

Dialog is a command-line tool used to generate interactive dialog boxes in text-based terminals. It is widely used in shell scripts to create interactive menus, confirmation boxes, forms, progress bars, among others. Dialog allows users to interact with a script through a text-based user interface, which is especially useful in server environments where a full graphical interface is not available.

Installing Dialog

To install Dialog on a Debian or Ubuntu-based distribution, simply run the following command:

sudo apt-get update
sudo apt-get install dialog

For Red Hat-based distributions like CentOS or Fedora:

sudo yum install dialog

Basic Examples of Dialog

Simple Message Box

This example shows a simple message box with only an “OK” button:

#!/bin/bash
dialog --title "Message" --msgbox "Hello, this is a simple message box." 6 50

Explanation: In this script, –title defines the dialog box title, –msgbox is the type of dialog used, and “6 50” are the dimensions of the box (6 lines high and 50 characters wide).

Interactive Menu

The following example creates a menu where the user can select an option:

#!/bin/bash
option=$(dialog --title "Main Menu" --menu "Select an option:" 15 50 4
1 "Option 1"
2 "Option 2"
3 "Option 3"
4 "Exit" 3>&1 1>&2 2>&3)
clear

echo "You selected option: $option"

Explanation: The menu is displayed with numbered options. 3>&1 1>&2 2>&3 is used to redirect the user’s selection back to the standard output.

Selection List

In this example, the user can select one or more items from a list:

#!/bin/bash
options=$(dialog --title "Package Selection" --checklist "Select the packages you want to install:" 15 50 5
1 "Apache" off
2 "MySQL" off
3 "PHP" off
4 "Python" off
5 "Java" off 3>&1 1>&2 2>&3)
clear

echo "Selected packages: $options"

Explanation: –checklist creates a list of items with checkboxes, where off indicates that the checkbox is unchecked by default.

Progress Bar

Progress bars are useful for showing the progress of a task. Here’s an example:

#!/bin/bash
{
for ((i = 0 ; i <= 100 ; i+=10)); do
sleep 1
echo $i
done
} | dialog --title "Progress" --gauge "Installing..." 10 70 0

Explanation: –gauge is used to create a progress bar. The for loop simulates the progress of a task, increasing the bar by 10% every second.

What is Whiptail?

Whiptail is a lightweight alternative to Dialog that also allows creating text-based interactive interfaces in shell scripts. Although Whiptail offers a similar set of features, it is especially useful in systems where Dialog is not available or where a lighter tool is preferred.

Installing Whiptail

To install Whiptail on Debian, Ubuntu, and their derivatives:

sudo apt-get update
sudo apt-get install whiptail

In distributions like CentOS, Red Hat, and Fedora:

sudo yum install newt

Basic Examples of Whiptail

Simple Message Box

As with Dialog, you can create a simple message box:

#!/bin/bash
whiptail --title "Message" --msgbox "This is a simple message using Whiptail." 8 45

Explanation: This example is similar to Dialog, but using Whiptail. The dimensions of the box are slightly different.

Interactive Menu

Creating interactive menus is easy with Whiptail:

#!/bin/bash
option=$(whiptail --title "Main Menu" --menu "Choose an option:" 15 50 4 \
"1" "Option 1" \
"2" "Option 2" \
"3" "Option 3" \
"4" "Exit" 3>&1 1>&2 2>&3)
clear

echo "You selected option: $option"

Explanation: This script works similarly to the Dialog example, allowing the user to select an option from a menu.

Selection List

Whiptail also allows creating selection lists with checkboxes:

#!/bin/bash
options=$(whiptail --title "Package Selection" --checklist "Select the packages you want to install:" 15 50 5 \
"Apache" "" ON \
"MySQL" "" OFF \
"PHP" "" OFF \
"Python" "" OFF \
"Java" "" OFF 3>&1 1>&2 2>&3)
clear

echo "Selected packages: $options"

Explanation: In this example, “ON” indicates that the checkbox is checked by default, unlike Dialog’s “off”.

Progress Bar

Finally, here’s an example of a progress bar with Whiptail:

#!/bin/bash
{
    for ((i = 0 ; i <= 100 ; i+=10)); do
        sleep 1
        echo $i
    done
} | whiptail --gauge "Installing..." 6 50 0

Explanation: This example is very similar to Dialog, but using Whiptail’s syntax.
Both Dialog and Whiptail are powerful and flexible tools that allow system administrators and developers to create interactive user interfaces within a terminal. Although both tools are similar in functionality, the choice between one or the other may depend on the specific needs of the system and personal preferences.

Dialog is more popular and widely documented, while Whiptail is a lighter alternative that may be preferred in systems where minimizing resource usage is crucial.

In this article, we have covered the basics of Dialog and Whiptail with practical examples that will allow you to start creating your own interactive scripts. Whether you need a simple menu, a message box, or a progress bar, these tools will provide the necessary functionalities to improve user interaction with your scripts.

Remember that the key to mastering these tools is practice. Try the examples provided, modify them to suit your needs, and continue exploring the many possibilities that Dialog and Whiptail offer to make your scripts more intuitive and user-friendly.

Video

Video Script

Below are two example scripts of two interactive menus:
Dialog

#!/bin/bash

# Example of a menu using Dialog
dialog --menu "Select an option:" 15 50 4 \
1 "View system information" \
2 "Show disk usage" \
3 "Configure network" \
4 "Exit" 2>selection.txt

# Read the selected option
option=$(cat selection.txt)

case $option in
    1)
        echo "Showing system information..."
        # Corresponding commands would go here
        ;;
    2)
        echo "Showing disk usage..."
        # Corresponding commands would go here
        ;;
    3)
        echo "Configuring network..."
        # Corresponding commands would go here
        ;;
    4)
        echo "Exiting..."
        exit 0
        ;;
    *)
        echo "Invalid option."
        ;;
esac

The result would be:

dialog
Whiptail

#!/bin/bash

# Example of a menu using Whiptail
option=$(whiptail --title "Main Menu" --menu "Select an option:" 15 50 4 \
"1" "View system information" \
"2" "Show disk usage" \
"3" "Configure network" \
"4" "Exit" 3>&1 1>&2 2>&3)

# Verify the selected option
case $option in
    1)
        echo "Showing system information..."
        # Corresponding commands would go here
        ;;
    2)
        echo "Showing disk usage..."
        # Corresponding commands would go here
        ;;
    3)
        echo "Configuring network..."
        # Corresponding commands would go here
        ;;
    4)
        echo "Exiting..."
        exit 0
        ;;
    *)
        echo "Invalid option."
        ;;
esac

With Whiptail, the result would be this:

whiptail
As you can see, the results are very similar.

References and Documentation

For Dialog and Whiptail, you can find extensive documentation at https://invisible-island.net/dialog/dialog.html

Leave a Reply