Text-based menus and information pages
Whip It Up
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.
News
-
Gnome Fans Everywhere Rejoice for the Latest Release
Gnome 47.2 is now available for general use but don't expect much in the way of newness, as this is all about improvements and bug fixes.
-
Latest Cinnamon Desktop Releases with a Bold New Look
Just in time for the holidays, the developer of the Cinnamon desktop has shipped a new release to help spice up your eggnog with new features and a new look.
-
Armbian 24.11 Released with Expanded Hardware Support
If you've been waiting for Armbian to support OrangePi 5 Max and Radxa ROCK 5B+, the wait is over.
-
SUSE Renames Several Products for Better Name Recognition
SUSE has been a very powerful player in the European market, but it knows it must branch out to gain serious traction. Will a name change do the trick?
-
ESET Discovers New Linux Malware
WolfsBane is an all-in-one malware that has hit the Linux operating system and includes a dropper, a launcher, and a backdoor.
-
New Linux Kernel Patch Allows Forcing a CPU Mitigation
Even when CPU mitigations can consume precious CPU cycles, it might not be a bad idea to allow users to enable them, even if your machine isn't vulnerable.
-
Red Hat Enterprise Linux 9.5 Released
Notify your friends, loved ones, and colleagues that the latest version of RHEL is available with plenty of enhancements.
-
Linux Sees Massive Performance Increase from a Single Line of Code
With one line of code, Intel was able to increase the performance of the Linux kernel by 4,000 percent.
-
Fedora KDE Approved as an Official Spin
If you prefer the Plasma desktop environment and the Fedora distribution, you're in luck because there's now an official spin that is listed on the same level as the Fedora Workstation edition.
-
New Steam Client Ups the Ante for Linux
The latest release from Steam has some pretty cool tricks up its sleeve.