In this tutorial we create a variety of dialog boxes in the Linux interactive shell script, the dialog box can be friendly to the operator in Linux, interested friends can refer to learning about.
When you install new software in the terminal environment, you can often see the Information dialog box pop-up and need your input. The types of dialog boxes are Lockbox, 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:
When you write an interactive shell script, you can use such a dialog box to accept the user's input. Whiptail can create terminal-based dialog boxes, message box procedures in shell scripts, similar to zenity or Xdialog GUI script code. Pre-installed in all Linux release versions.
Let's look at the usage of Whiptail:
Create a message box
A message box displays a confirmation button to continue any text message.
Grammar:
Whiptail–title "" –msgbox ""
Instance:
#!/bin/"Test message box""Create a Message box with Whiptail. Choose Ok to continue. " Ten -
Create a yes/no dialog box
A dialog box where 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." 10 60)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?" 10 60) then
echo "You chose Skittles Exit status was $?."
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?" 10 60 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." 10 60 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
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 a Table dialog box
The Radiolist dialog box is useful when you want to let the user select a list of multiple options in the Checklist dialog box, allowing only one to select.
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 <= 100 ; i+=20)); do
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 interactive shell scripts. Next time you need to write an interactive shell script, try to use the Whiptail ha
This article is from: a walkthrough of creating a Dialog box in a Linux interactive shell script Tutorial _linux Server
Walkthrough of Creating a Dialog box in a Linux interactive shell script Tutorial _linux Server