How to use the command 'whiptail' (with examples)

How to use the command 'whiptail' (with examples)

Whiptail is a versatile command that allows shell scripts to display text-based dialog boxes, providing interactive user interfaces in a terminal environment. It offers various options for displaying messages, choices, input boxes, and menus to gather user input or display information. This article will illustrate each of the following use cases of the ‘whiptail’ command.

Use case 1: Display a simple message

Code:

whiptail --title "Simple Message" --msgbox "This is a simple message box." 10 30

Motivation: This use case is useful when you want to display an informative message to the user. The simple message box is non-interactive and provides a way to convey information.

Explanation:

  • --title: Specifies the title of the dialog box.
  • --msgbox: Instructs ‘whiptail’ to display a message box.
  • “This is a simple message box.”: The message to be displayed to the user.
  • 10: The height of the dialog box in characters.
  • 30: The width of the dialog box in characters.

Example output: A dialog box titled “Simple Message” will appear with the message “This is a simple message box.”

Use case 2: Display a boolean choice

Code:

whiptail --title "Boolean Choice" --yesno "Do you agree?" 10 30

Motivation: This use case is useful when you need to present a simple yes-or-no question to the user and retrieve their response as an exit code.

Explanation:

  • --title: Specifies the title of the dialog box.
  • --yesno: Instructs ‘whiptail’ to display a dialog box with a yes/no choice.
  • “Do you agree?”: The question to be displayed to the user.
  • 10: The height of the dialog box in characters.
  • 30: The width of the dialog box in characters.

Example output: A dialog box titled “Boolean Choice” will appear with the question “Do you agree?” The user can select either yes or no. The exit code will be 0 if they choose yes, and 1 if they select no.

Use case 3: Customise the text on the yes/no buttons

Code:

whiptail --title "Custom Buttons" --yes-button "Agree" --no-button "Disagree" --yesno "Do you agree?" 10 50

Motivation: This use case is useful when you want to customize the text on the yes and no buttons to align with the context of the question being asked.

Explanation:

  • --title: Specifies the title of the dialog box.
  • --yes-button: Specifies the text to display on the yes button.
  • --no-button: Specifies the text to display on the no button.
  • --yesno: Instructs ‘whiptail’ to display a dialog box with a customized yes/no choice.
  • “Do you agree?”: The question to be displayed to the user.
  • 10: The height of the dialog box in characters.
  • 50: The width of the dialog box in characters.

Example output: A dialog box titled “Custom Buttons” will appear with the question “Do you agree?” The user can choose between “Agree” or “Disagree” buttons.

Use case 4: Display a text input box

Code:

result_variable_name="$(whiptail --title "Text Input" --inputbox "Enter your name:" 10 30 John 3>&1 1>&2 2>&3)"

Motivation: This use case is useful when you want to obtain user input as text and save it to a variable for further processing within a shell script.

Explanation:

  • result_variable_name: The variable to store the user’s input.
  • --title: Specifies the title of the dialog box.
  • --inputbox: Instructs ‘whiptail’ to display an input box.
  • “Enter your name:”: The message to be displayed as a prompt.
  • 10: The height of the dialog box in characters.
  • 30: The width of the dialog box in characters.
  • John: The default text to be displayed in the input box.
  • 3>&1 1>&2 2>&3: Redirects the standard output and error streams to capture the input.

Example output: A dialog box titled “Text Input” will appear with the message “Enter your name:”. The user can type their name, and the value will be stored in the result_variable_name variable.

Use case 5: Display a password input box

Code:

result_variable_name="$(whiptail --title "Password Input" --passwordbox "Enter your password:" 10 30 3>&1 1>&2 2>&3)"

Motivation: This use case is useful when you want to prompt the user to enter a password securely, without displaying the entered characters on the screen.

Explanation:

  • result_variable_name: The variable to store the user’s input.
  • --title: Specifies the title of the dialog box.
  • --passwordbox: Instructs ‘whiptail’ to display a password input box.
  • “Enter your password:”: The message to be displayed as a prompt.
  • 10: The height of the dialog box in characters.
  • 30: The width of the dialog box in characters.
  • 3>&1 1>&2 2>&3: Redirects the standard output and error streams to capture the input.

Example output: A dialog box titled “Password Input” will appear with the message “Enter your password:”. The user can type their password, and the value will be stored in the result_variable_name variable.

Use case 6: Display a multiple-choice menu

Code:

result_variable_name=$(whiptail --title "Menu" --menu "Choose your favorite fruit:" 15 40 3 \
"1" "Apple" \
"2" "Banana" \
"3" "Orange" \
"4" "Strawberry" \
"5" "Watermelon" \
3>&1 1>&2 2>&3)

Motivation: This use case is useful when you want to present the user with a menu of options and capture their selection as a return value.

Explanation:

  • result_variable_name: The variable to store the user’s selected value.
  • --title: Specifies the title of the dialog box.
  • --menu: Instructs ‘whiptail’ to display a menu.
  • “Choose your favorite fruit:”: The message to be displayed as a prompt.
  • 15: The height of the dialog box in characters.
  • 40: The width of the dialog box in characters.
  • 3: The number of menu items.
  • “1” “Apple”, “2” “Banana”, “3” “Orange”, etc.: The value and display text for each menu item.
  • 3>&1 1>&2 2>&3: Redirects the standard output and error streams to capture the input.

Example output: A dialog box titled “Menu” will appear with the message “Choose your favorite fruit:”. The user can navigate through the menu using arrow keys and press Enter to select an item. The selected value will be stored in the result_variable_name variable.

Conclusion:

The ‘whiptail’ command is a powerful tool for creating interactive text-based dialog boxes in shell scripts. It offers various options to display messages, choices, input boxes, and menus, providing a user-friendly experience in a terminal environment. By understanding and utilizing the different use cases demonstrated in this article, you can enhance the interactivity and usability of your shell scripts.

Related Posts

Working with AWS Route53 (with examples)

Working with AWS Route53 (with examples)

Introduction AWS Route53 is a highly available and scalable Domain Name System (DNS) web service provided by Amazon Web Services.

Read More
How to use the command "dust" (with examples)

How to use the command "dust" (with examples)

Dust is a command-line tool that provides an instant overview of which directories are using disk space.

Read More
Using the mixxx Command (with examples)

Using the mixxx Command (with examples)

Mixxx is a free and open-source cross-platform DJ software that provides various command-line options for different functionalities.

Read More