Detailed Linux Interactive shell script Create dialog Example tutorial

Source: Internet
Author: User

When you install new software in a terminal environment, you can often see the Information dialog box pop up and need your input. The type of dialog box has a password box, checklist, menu, and so on. They can guide you in an intuitive way to enter the necessary information, and the benefits of using such a user-friendly dialog box are obvious. As shown in the following illustration:

When you write an interactive shell script, you can use such a dialog box to accept the user's input. Whiptail can create a terminal-based dialog box in a shell script, a message box process that resembles a zenity or xdialog GUI scripting code. Pre-installed in all Linux release versions.

Here's a look at the use of Whiptail:

Create a message box

A message box displays a confirmation button to continue any text message.

Grammar:

Whiptail–title "" –msgbox ""

Instance:

#!/bin/bash
Whiptail--title ' Test message box '--msgbox ' Create a message box with Whiptail. Choose OK to continue. " 10 60

Create a yes/no dialog box

A dialog box in which the user enters Yes or No.

Grammar:

Whiptail–title "" –yesno ""

Instance:

#!/bin/bash
if (whiptail--title "Test yes/no box"--yesno "Choose between Yes and No." Then)
echo "You chose Yes." Exit status was $?.
Else
echo "You chose No. Exit status was $?.
Fi

Alternatively, you can be "–yes-button", "–no-button" option.

#!/bin/bash
if (whiptail--title "Test yes/no box"--yes-button "Skittles"--no-button "m&m" S "--yesno" Which do you like better? "Then"
echo "You chose Skittles Exit status is $?."
Else
echo "You chose M&m ' s. Exit status was $?.
Fi

Create a form input box

If you want the user to enter any text, you can use an input box.

Grammar:

Whiptail–title "" –inputbox ""

Instance:

#!/bin/bash
pet=$ (whiptail--title "Test free-form Input Box"--inputbox "What is your pet ' s name?" Wigglebutt 3>&1 1>&2 2>&3)

Exitstatus=$?
if [$exitstatus = 0]; Then
echo "Your pet name is:" $PET
Else
echo "You chose Cancel."
Fi

Create a password box

The password box is useful when the user needs to enter sensitive information.

Grammar:

Whiptail–title "" –passwordbox ""

Instance:

#!/bin/bash
password=$ (whiptail--title "Test PASSWORD box"--passwordbox "Enter your PASSWORD and choose OK to continue." 3>&1 1>&2 2>&3)

Exitstatus=$?
if [$exitstatus = 0]; Then
echo "Your password is:" $PASSWORD
Else
echo "You chose Cancel."
Fi

Create a menu bar

When you want the user to choose an arbitrary number of choices, you can use the menu box.

Grammar:

Whiptail–title "

"–menu" "

[ ] . . .

Instance:

#!/bin/bash
option=$ (Whiptail--title "Test menu Dialog"--menu "Choose your Option" 15 60 4
"1" "Grilled spicy sausage"
"2" "Grilled halloumi Cheese"
"3" "Charcoaled Chicken Wings"
"4" "Fried aubergine" 3>&1 1>&2 2>&3)

Exitstatus=$?
if [$exitstatus = 0]; Then
echo "Your chosen option:" $OPTION
Else
echo "You chose Cancel."
Fi

Create Radiolist dialog box

Grammar:

Whiptail–title "" –radiolist "[] ...

Instance:

#!/bin/bash
distros=$ (whiptail--title "Test checklist Dialog"--radiolist
"What is the Linux distro of your choice?" 15 60 4
"Debian" "Venerable Debian" on
"Ubuntu" "popular Ubuntu" off
"CentOS" "Stable CentOS" off
"Mint" "Rising Star Mint" off 3>&1 1>&2 2>&3)

Exitstatus=$?
if [$exitstatus = 0]; Then
echo "The Chosen distro is:" $DISTROS
Else
echo "You chose Cancel."
Fi

Create a Table dialog box

When you want the user to select a list of multiple options in the Checklist dialog box is useful, the Radiolist dialog box, only allows you to select one.

Grammar:

Whiptail–title "" –checklist "[] ...

Instance:

#!/bin/bash
distros=$ (whiptail--title "Test checklist Dialog"--checklist
"Choose preferred Linux distros" 15 60 4
"Debian" "Venerable Debian" on
"Ubuntu" "popular Ubuntu" off
"CentOS" "Stable CentOS" on
"Mint" "Rising Star Mint" off 3>&1 1>&2 2>&3)

Exitstatus=$?
if [$exitstatus = 0]; Then
echo "Your favorite distros are:" $DISTROS
Else
echo "You chose Cancel."
Fi

Create a progress bar

The progress bar is a user-friendly dialog box. Whiptail reads a percentage (0~100) from the standard input, showing the corresponding count within a table.

Grammar:

Whiptail–gauge ""

Instance:

#!/bin/bash
{
for ((i = 0; I <= i+=20)); Todo
Sleep 1
Echo $i
Done
} | Whiptail--gauge "Please wait while installing" 6 60 0

Haha, how easy it is to create useful dialog boxes in an interactive shell script. The next time you need to write an interactive shell script, try using Whiptail.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.