- Posted on
- • Advanced
Creating interactive scripts with dialog or zenity
- Author
-
-
- User
- Linux Bash
- Posts by this author
- Posts by this author
-
Creating Interactive Scripts in Linux with Dialog and Zenity
Whether you're administering servers or working on your personal Linux box, crafting efficient and user-friendly shell scripts can massively simplify complex tasks. While traditional Bash scripts do the job, adding a graphical interface can make scripts intuitive and accessible, even for those less acquainted with the command line. In this blog post, we'll delve into how to create interactive scripts using dialog
for terminal-based interfaces and zenity
for graphical (GUI) dialog boxes.
Installing Dialog and Zenity
Before we start creating scripts, let's ensure that both dialog
and zenity
are installed on your system. Since different Linux distributions use different package managers, installation commands vary:
For Debian-based systems (using apt
):
sudo apt update
sudo apt install dialog zenity
For Fedora and similar (using dnf
):
sudo dnf install dialog zenity
For openSUSE (using zypper
):
sudo zypper install dialog zenity
Creating a Script with Dialog
dialog
is a utility for creating text-based user interfaces within a shell environment. Let’s create a simple script to understand how it works:
Create a new Bash script:
nano disk_check.sh
Add the following content to create a basic menu using dialog:
#!/bin/bash cmd=(dialog --separate-output --checklist "Select options:" 22 76 16) options=( 1 "Check disk space" on 2 "List users currently logged in" off 3 "Show system uptime" off ) choices=$("${cmd[@]}" "${options[@]}" 2>&1 >/dev/tty) clear for choice in $choices do case $choice in 1) df -h ;; 2) who ;; 3) uptime ;; esac done
Make the script executable and run it:
chmod +x disk_check.sh ./disk_check.sh
This script displays a checklist using dialog
, allowing users to select options for displaying disk space, logged users, and system uptime.
Crafting a GUI with Zenity
Zenity transpires as a counterpart to dialog
but targets graphical environments, using GTK+ for rendering forms. Here’s how to script a similar functionality using Zenity.
Create a new script:
nano gui_disk_check.sh
Write a Zenity script:
#!/bin/bash ans=$(zenity --list --title "System Check" --checklist \ --column "Pick" --column "Operation" \ TRUE "Check disk space" FALSE "List users" FALSE "Show uptime" \ --separator=":") IFS=":" read -ra ADDR <<< "$ans" for i in "${ADDR[@]}"; do case $i in "Check disk space") zenity --info --text="$(df -h)" ;; "List users") zenity --info --text="$(who)" ;; "Show uptime") zenity --info --text="$(uptime)" ;; esac done
Make it executable and run:
chmod +x gui_disk_check.sh ./gui_disk_check.sh
This script uses Zenity to present a graphical checklist and displays the selected information in a GUI dialog.
Conclusion
Both dialog
and zenity
provide potent mechanisms for enhancing the interactivity and user-friendliness of shell scripts. dialog
is ideal for text-only environments, while zenity
excels in graphical desktop environments. By leveraging these tools, you can build flexible, intuitive scripts suitable for a wide range of users and applications. Experiment with these to better adapt your scripts to user needs!