转贴自:http://xmodulo.com/create-dialog-boxes-interactive-shell-script.html

When you install new software in the terminal environment, you mayoften see informative dialog boxes popping up, accepting your input.The type of dialog boxes ranges from simple yes/no dialog to input box,password box, checklist, menu, and so on. The advantage of using suchuser-friendly dialog boxes is obvious as they can guide you to enternecessary information in an intuitive fashion.

How to create dialog boxes in an interactive shell script-LMLPHP

When you write an interactive shell script, you can actually use such dialog boxes to take user's input. Pre-installed on all modern Linuxdistributions, a program called whiptail can streamline theprocess of creating terminal-based dialogs and message boxes inside ashell script, similar to how Zenity or Xdialog codes a GUI for scripts.

In this tutorial, I describe how to create user-friendly dialog boxes in a shell script by using whiptail. I also show Bash code snippets of various dialog boxes supported by whiptail.

Create a Message Box

A message box shows any arbitrary text message with a confirmation button to continue.

whiptail --title "" --msgbox ""

Example:

点击(此处)折叠或打开

  1. #!/bin/bash
  2. shopt -s -o nounset

  3. whiptail --title "Test Message Box" --msgbox "Create a message box with whiptail. Choose Ok to continue." 10 60


How to create dialog boxes in an interactive shell script-LMLPHP

Create a Yes/No Box

One common user input is Yes or No. This is when a Yes/No dialog box can be used.

whiptail --title "" --yesno ""

Example:

点击(此处)折叠或打开

  1. #!/bin/bash

  2. shopt -s -o nounset
  3. if (whiptail --title "Test Yes/No Box" --yesno "Choose between Yes and No." 10 60) then
  4.     echo "You chose Yes. Exit status was $?."
  5. else
  6.     echo "You chose No. Exit status was $?."
  7. fi


How to create dialog boxes in an interactive shell script-LMLPHP

Optionally, you can customize the text for Yes and No buttons with "--yes-button" and "--no-button" options.

Example:

点击(此处)折叠或打开

  1. #!/bin/bash

  2. shopt -s -o nounset
  3. if (whiptail --title "Test Yes/No Box" --yes-button "Skittles" --no-button "M&M's" --yesno "Which do you like better?" 10 60) then
  4.     echo "You chose Skittles Exit status was $?."
  5. else
  6.     echo "You chose M&M's. Exit status was $?."
  7. fi


How to create dialog boxes in an interactive shell script-LMLPHP

Create a Free-form Input Box

If you want to take any arbitrary text input from a user, you can use an input box.

whiptail --title "" --inputbox ""

Example:

点击(此处)折叠或打开

  1. #!/bin/bash

  2. shopt -s -o nounset
  3. PET=$(whiptail --title "Test Free-form Input Box" --inputbox "What is your pet's name?" 10 60 Wigglebutt 3>&1 1>&2 2>&3)
  4.  
  5. exitstatus=$?
  6. if [ $exitstatus = 0 ]; then
  7.     echo "Your pet name is:" $PET
  8. else
  9.     echo "You chose Cancel."
  10. fi


How to create dialog boxes in an interactive shell script-LMLPHP

Create a Password Box

A password box is useful when you want to take a sensitive input from a user.

whiptail --title "" --passwordbox ""

Example:

点击(此处)折叠或打开

  1. #!/bin/bash

  2. shopt -s -o nounset

  3. PASSWORD=$(whiptail --title "Test Password Box" --passwordbox "Enter your password and choose Ok to continue." 10 60 3>&1 1>&2 2>&3)
  4.  
  5. exitstatus=$?
  6. if [ $exitstatus = 0 ]; then
  7.     echo "Your password is:" $PASSWORD
  8. else
  9.     echo "You chose Cancel."
  10. fi


How to create dialog boxes in an interactive shell script-LMLPHP

Create a Menu Box

When you want to ask a user to choose one among any arbitrary number of choices, you can use a menu box.

whiptail --title "

	" --menu ""

		[   ] . . .




Example:

点击(此处)折叠或打开

  1. #!/bin/bash

  2. shopt -s -o nounset

  3. OPTION=$(whiptail --title "Test Menu Dialog" --menu "Choose your option" 15 60 4 \
  4. "1" "Grilled Spicy Sausage" \
  5. "2" "Grilled Halloumi Cheese" \
  6. "3" "Charcoaled Chicken Wings" \
  7. "4" "Fried Aubergine" 3>&1 1>&2 2>&3)
  8.  
  9. exitstatus=$?
  10. if [ $exitstatus = 0 ]; then
  11.     echo "Your chosen option:" $OPTION
  12. else
  13.     echo "You chose Cancel."
  14. fi


How to create dialog boxes in an interactive shell script-LMLPHP

Create a Radiolist Dialog

A radiolist box is similar to a menu box in the sense that you canchoose only option among a list of available options. Unlike a menu box, however, you can indicate which option is selected by default byspecifying its status.

whiptail --title "" --radiolist ""    [    ] . . .

Example:

点击(此处)折叠或打开

  1. #!/bin/bash

  2. shopt -s -o nounset

  3. DISTROS=$(whiptail --title "Test Checklist Dialog" --radiolist \
  4. "What is the Linux distro of your choice?" 15 60 4 \
  5. "debian" "Venerable Debian" ON \
  6. "ubuntu" "Popular Ubuntu" OFF \
  7. "centos" "Stable CentOS" OFF \
  8. "mint" "Rising Star Mint" OFF 3>&1 1>&2 2>&3)
  9.  
  10. exitstatus=$?
  11. if [ $exitstatus = 0 ]; then
  12.     echo "The chosen distro is:" $DISTROS
  13. else
  14.     echo "You chose Cancel."
  15. fi


How to create dialog boxes in an interactive shell script-LMLPHP

Create a Checklist Dialog

A checklist dialog is useful when you want to ask a user to choosemore than one option among a list of options, which is in contrast to aradiolist box which allows only one selection.

whiptail --title "" --checklist ""    [    ] . . .

Example:

点击(此处)折叠或打开

  1. #!/bin/bash

  2. shopt -s -o nounset

  3. DISTROS=$(whiptail --title "Test Checklist Dialog" --checklist \
  4. "Choose preferred Linux distros" 15 60 4 \
  5. "debian" "Venerable Debian" ON \
  6. "ubuntu" "Popular Ubuntu" OFF \
  7. "centos" "Stable CentOS" ON \
  8. "mint" "Rising Star Mint" OFF 3>&1 1>&2 2>&3)
  9.  
  10. exitstatus=$?
  11. if [ $exitstatus = 0 ]; then
  12.     echo "Your favorite distros are:" $DISTROS
  13. else
  14.     echo "You chose Cancel."
  15. fi


How to create dialog boxes in an interactive shell script-LMLPHP

Create a Progress Bar

Another user-friendly dialog box is a progress bar. whiptail reads from standard input a percentage number (0 to 100) and displays a meter inside a gauge box accordingly.
Example: whiptail --gauge ,这里和dialog是不同的,dialog 的命令是dialog --guage. 一字之差.
下面有两个例子:

点击(此处)折叠或打开

  1. #!/bin/bash

  2. shopt -s -o nounset

  3. {
  4.     for ((i = 0 ; i <= 100 ; i+=20)); do
  5.         sleep 1
  6.         echo $i
  7.     done
  8. } | whiptail --gauge "Please wait while installing" 6 60 0


点击(此处)折叠或打开

  1. #!/bin/bash

  2. shopt -s -o nounset

  3. {
  4. for ((i=1;i<=10;i++))
  5. do
  6.     let I=10*i
  7.     echo $I
  8.     sleep 1
  9. done
  10. echo

  11. for ((i=9;i>=0;i--))
  12. do
  13.     let I=10*i
  14.     echo $I
  15.     sleep 1
  16. done

  17. } | whiptail --gauge "安装进度" 5 60 0


How to create dialog boxes in an interactive shell script-LMLPHP

By now, you must see how easy it is to create useful dialog boxes inan interactive shell script. Next time you need to write a shell script for someone, why don't you try whiptail and impress him or her? :-)

10-12 15:23