Text-based menus and information pages
Whip It Up
![© Lead Image © PazMadrid, morguefile.com © Lead Image © PazMadrid, morguefile.com](/var/linux_magazin/storage/images/issues/2023/270/whiptail/pazmadrid_morguefile-typesetting.png/821269-1-eng-US/PazMadrid_morguefile-Typesetting.png_medium.png)
© Lead Image © PazMadrid, morguefile.com
Whiptail interfaces add menus and information pages to your Raspberry Pi projects.
Text interfaces can be extremely useful for Windows or secure shell (SSH) clients that need to connect to your Linux or Raspberry Pi systems. One of the simplest options for creating text interfaces is Whiptail [1], which comes preinstalled on Raspbian and many Linux systems. The Raspberry Pi configuration tool, raspi-config
(Figure 1), is a good illustration of how Whiptail can be used.
In this article, I introduce Whiptail with three small projects. The first creates a menu interface for some common Linux diagnostic tools, the second controls Raspberry Pi output pins, and the final uses Python to display Pi sensor data.
Getting Started
If your system does not have Whiptail, you can install it on Raspbian/Debian/Ubuntu machines with the command:
sudo apt install whiptail
A simple Whiptail test shows the date/time in a message box:
whiptail --title "Time/Date" --msgbox "$(date)" 0 0
This statement clears the terminal screen and shows a default background with a message box centered in the window (Figure 2). The last two parameters in the statement define the height and width of the dialog. The 0 0 at the end of the last line autosizes the message box.
The Whiptail utility supports a variety of controls, such as: checklist
, gauge
, infobox
, inputbox
, menu
, msgbox
, passwordbox
, radiolist
, textbox
, and yesno
.
A Menu of Diagnostic Tools
Menuing is one of most useful features in Whiptail. A main script can present a top-level menuing interface that links to submenus, information pages, or applications. The syntax for the menu options is:
[...] --menu <height> <width> <listheight> [<tag> <item>]
The <item>
parameter is the text string shown on the menu line. The <tag>
parameter is the variable passed when a menu is selected; this parameter is typically a number, but you can also use strings. In my menuing example, I will be passing the diagnostic utility name as the tag.
Listing 1 is a Whiptail menuing script that shows three common Linux diagnostic tools, df
(disk space usage), vmstat
(virtual memory statistics), and lsusb
(list USB devices). For this example, a fourth menu line exits the script (Figure 3). The --menu
option is defined to autosize with four menu items (0 0 4
; line 9). Each menu has a line description and a tag that is passed when that line is selected.
Listing 1
Whiptail Menus
01 #!/bin/bash 02 # 03 # wmenu.sh - Wipetail Menu example with diagnostic tools 04 # 05 06 while true 07 do 08 # Create a whiptail menu, pass the tool name if selected 09 selection=$(whiptail --title "Diagnostics" --nocancel --menu "Select Tool:" 0 0 4 "df" " Disk Space" "vmstat" " Memory Stats" "lsusb" " USB Devices" "Exit" " Close and Exit" 3>&1 1>&2 2>&3 ) 10 11 # Do an action for the selected menu item 12 case $selection in 13 "df" ) 14 whiptail --title "Disk Space" --msgbox "$(df -h)" 0 0 15 ;; 16 "vmstat" ) 17 whiptail --title "Memory Stats" --msgbox "$(vmstat --stats)" 0 0 18 ;; 19 "lsusb" ) 20 whiptail --title "USB Devices" --msgbox "$(lsusb)" 0 0 21 ;; 22 "Exit" ) 23 exit 24 ;; 25 esac 26 done
For menuing applications the Whiptail utility requires a set of redirection statements, ( 3>&1 1>&2 2>&3 ) at the end of the main command. These redirections are a Bash trick to swap standard output, (file 1, stdout), and standard error (file 2, stderr). By adding these statements Whiptail has a clean, error free, mechanism for user input and menu output.
This menu example only uses an Ok button, the Cancel button is disabled by the --nocancel
parameter (line 9). In the next example the Cancel button will exit the script.
Raspberry Pi Radiolist Project
The goal of the Raspberry Pi radiolist project is to create a simple user interface that controls general purpose input/output (GPIO) pins. For my setup I am using a Pimoroni Explorer HAT (hardware attached on top) that has four LEDs; however, other hardware arrangements could also be used.
The first step for this project is to install wiringPi [2], so that GPIO pins can be accessed and controlled from Bash scripts. This utility is installed on a Raspberry Pi with:
sudo apt-get install git-core git clone https://github.com/WiringPi/WiringPi.git cd WiringPi git pull origin ./build
The wiringpi library includes the gpio
command-line utility that you can use to read, write, toggle, and set the mode of GPIO pins. My hardware arrangement has four LEDs on pins 0, 2, 7, and 21 that I need to set up for output:
# Set the LED pins as outputs gpio mode 7 output gpio mode 0 output gpio mode 2 output gpio mode 21 output
Once the GPIO pins are set up as outputs, I can toggle their states manually:
# Toggle the output state on pin 7 gpio toggle 7
The syntax of a radiolist
is similar to a menu:
--radiolist <height> <width> <list-height> [ <tag> <item> <status> ]
The only difference with a checklist is that you can indicate which entry is currently selected by setting its status to on
. The default radiolist
has two buttons, an Ok, which returns a
when selected, and a Cancel button, which returns a 1
.
The Raspberry Pi radiolist script (Listing 2) shows a list of GPIO pins the user can toggle (Figure 4). A while
statement loops as long as the Ok button is selected (lines 9 and 14). The radiolist is defined to show a GPIO pin in the <tag>
parameter, and the <item>
parameter displays the LED number, color, and physical pin number (lines 11). An if
statement checks a GPIO pin (line 17) and then passes a valid pin number to the gpio toggle
statement (line 19).
Listing 2
Whiptail Radiolist
01 #!/usr/bin/bash 02 # 03 # wradio.sh - toggle a GPIO output pin 04 # 05 OK=0 06 response="$OK" 07 08 # Cycle OK entered, Enter Cancel to exit 09 while [ "$response" == "$OK" ] ; do 10 11 thepin=$(whiptail --title "Toggle GPIO Pins" --backtitle "Raspberry PI GPIO" --radiolist "Select Pin:" 0 0 4 7 "GPIO_7 - LED 1 (BLUE) physical pin 7 " off 0 "GPIO_0 - LED 2 (YELLOW) physical pin 11" off 2 "GPIO_2 - LED 3 (RED) physical pin 13" off 21 "GPIO_7 - LED 4 (GREEN) physical pin 29 " off 3>&1 1>&2 2>&3 ) 12 13 # Get the response from the radio dialog 14 response=$? ; # Get the output from the dialog 15 16 # Check if a radio item returned a value 17 if [ ${#thepin} > 0 ] ; then 18 # Toggle the selected GPIO pin number 19 gpio toggle $thepin 20 fi 20 done
This project could be enhanced with a checklist
box that shows the present status of all GPIO pins; then, rather than setting one pin, multiple pins could be set or reset in a single pass.
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
![Learn More](https://www.linux-magazine.com/var/linux_magazin/storage/images/media/linux-magazine-eng-us/images/misc/learn-more/834592-1-eng-US/Learn-More_medium.png)
News
-
NVIDIA Released Driver for Upcoming NVIDIA 560 GPU for Linux
Not only has NVIDIA released the driver for its upcoming CPU series, it's the first release that defaults to using open-source GPU kernel modules.
-
OpenMandriva Lx 24.07 Released
If you’re into rolling release Linux distributions, OpenMandriva ROME has a new snapshot with a new kernel.
-
Kernel 6.10 Available for General Usage
Linus Torvalds has released the 6.10 kernel and it includes significant performance increases for Intel Core hybrid systems and more.
-
TUXEDO Computers Releases InfinityBook Pro 14 Gen9 Laptop
Sporting either AMD or Intel CPUs, the TUXEDO InfinityBook Pro 14 is an extremely compact, lightweight, sturdy powerhouse.
-
Google Extends Support for Linux Kernels Used for Android
Because the LTS Linux kernel releases are so important to Android, Google has decided to extend the support period beyond that offered by the kernel development team.
-
Linux Mint 22 Stable Delayed
If you're anxious about getting your hands on the stable release of Linux Mint 22, it looks as if you're going to have to wait a bit longer.
-
Nitrux 3.5.1 Available for Install
The latest version of the immutable, systemd-free distribution includes an updated kernel and NVIDIA driver.
-
Debian 12.6 Released with Plenty of Bug Fixes and Updates
The sixth update to Debian "Bookworm" is all about security mitigations and making adjustments for some "serious problems."
-
Canonical Offers 12-Year LTS for Open Source Docker Images
Canonical is expanding its LTS offering to reach beyond the DEB packages with a new distro-less Docker image.
-
Plasma Desktop 6.1 Released with Several Enhancements
If you're a fan of Plasma Desktop, you should be excited about this new point release.