Skip to content

Whiptail (Dialog)

Nice dialog option that can be used in bash scripts.

Bash
whiptail --title "Some Tittle" --msgbox --ok-button Sure "Welcome! Let's Begin." 8 78

exitstatus=$?
if [ $exitstatus = 255 ]; then
  exit;
fi

# Domain Name

DOMAIN=$(whiptail --inputbox --ok-button Add "Please enter domain name (FQDN)" 8 39 example.domain.com --title "Enter FQDN (Domain)"  3>&1 1>&2 2>&3)

exitstatus=$?
if [ $exitstatus = 1 ]; then
  exit;
fi

echo $DOMAIN
Bash
#!/bin/bash

# Define a function to display the main menu
function show_menu {
  # Use whiptail to display the menu options
  selection=$(whiptail --title "Main Menu" --menu "Choose an option:" 15 60 3 \
    "Option 1" "Description of option 1" \
    "Quit" "Exit the program" 3>&1 1>&2 2>&3)
  exitstatus=$?

  # Check if the user cancelled the menu
  if [ $exitstatus = 1 ]; then
    exit 0
  fi

  # Call a function based on the selected option
  case "$selection" in
    "Option 1") show_submenu;;
    "Quit") exit 0;;
  esac
}

# Define a function to display the submenu for option 1
function show_submenu {
  # Use whiptail to display the menu options
  selection=$(whiptail --title "Option 1 Menu" --menu "Choose an option:" 15 60 3 \
    "Suboption 1" "Description of suboption 1" \
    "Back" "Go back to the main menu" 3>&1 1>&2 2>&3)
  exitstatus=$?

  # Check if the user selected "Back"
  if [ "$selection" = "Back" ]; then
    show_menu
  fi

  # Check if the user cancelled the menu
  if [ $exitstatus = 1 ]; then
    exit 0
  fi

  # Call a function based on the selected option
  case "$selection" in
    "Suboption 1") suboption1;;
  esac
}

# Define a function for the submenu option
function suboption1 {
  # Display a message using whiptail
  whiptail --title "Suboption 1" --msgbox "You selected suboption 1. Press OK to go back to the main menu." 10 60
  show_menu
}

# Display the main menu when the script is run
show_menu