Displaying GUI Boxes in Linux Bash scripts can be done easily using whiptail.
In Windows OS, msgbox, inptubox is quite helpful on VBA, Visual Basic and even in .NET world.
Displaying Graphical boxes in Linux bash scripting is quite simple to do.
To navigate on this GUI boxes, used "Tabs","Arrow Keys" and "Space Bar" as enter key.
Here's a simple example of Whiptail that display GUI Boxes and ping the sites that are selected.
#!/bin/bash
# Step 1: Checkbox URL menu
CHOICES=$(whiptail --title "Checkbox Ping URLs Example" \
--checklist "Select one or more options:" 15 50 5 \
"ping -c 2 bing.com;" "Ping bing" OFF \
"ping -c 2 duckduckgo.com;" "Ping ddg" OFF \
"ping -c 2 search.com;" "Ping search" OFF \
"ping -c 2 google.com" "Ping google" OFF \
3>&1 1>&2 2>&3)
# Exit gracefully if nothing was selected or user pressed Cancel
if [ -z "$CHOICES" ]; then
echo "No urls selected or operation canceled."
exit 0
fi
# Step 2: Clean up whiptail quotes so we get valid bash commands
COMMANDS=$(echo "$CHOICES" | tr -d '"')
# Step 3: Confirm execution
whiptail --title "Starting Pings" \
--msgbox "The following commands will run:\n\n$COMMANDS" 15 50
# Step 4: Run each ping command
echo -e "\n--- Executing Ping Commands ---\n"
IFS=';' read -ra CMD_LIST <<< "$COMMANDS"
for cmd in "${CMD_LIST[@]}"; do
# Skip empty entries
cmd=$(echo "$cmd" | xargs)
if [ -n "$cmd" ]; then
echo "Running: $cmd"
eval "$cmd"
echo "-----------------------------------"
fi
done
The code will display the GUI Boxes like the images below:
CheckBox GUI prompt for selection:
If whiptail is not in the system, it can easily be installed.
That's it till next time.. Enjoy exploring command lines.
Grow your Faith and Love for God each day.
The world you know is passing right before your eyes.
Time is a limited edition.
Visit: Fiat Voluntas Tua | Catholic Prayers & Quotes for Prayers/Reflections



Comments
Post a Comment