Linuxer, ex IBMer. GNU https://hmchzb19.github.io/
全部博文(297)
分类: LINUX
2017-06-09 10:28:21
转贴自:
When you install new software in the terminal environment, you may
often 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 such
user-friendly dialog boxes is obvious as they can guide you to enter
necessary information in an intuitive fashion.
When you write an interactive shell script, you can actually use such dialog boxes to take user's input. Pre-installed on all modern Linux distributions, a program called whiptail can streamline the process of creating terminal-based dialogs and message boxes inside a shell 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.
A message box shows any arbitrary text message with a confirmation button to continue.
whiptail --title "" --msgbox ""
Example:
点击(此处)折叠或打开
One common user input is Yes or No. This is when a Yes/No dialog box can be used.
whiptail --title "" --yesno ""
Example:
点击(此处)折叠或打开
Optionally, you can customize the text for Yes and No buttons with "--yes-button" and "--no-button" options.
Example:
点击(此处)折叠或打开
If you want to take any arbitrary text input from a user, you can use an input box.
whiptail --title "" --inputbox ""
Example:
点击(此处)折叠或打开
A password box is useful when you want to take a sensitive input from a user.
whiptail --title "" --passwordbox ""
Example:
点击(此处)折叠或打开
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:
点击(此处)折叠或打开
A radiolist box is similar to a menu box in the sense that you can choose only option among a list of available options. Unlike a menu box, however, you can indicate which option is selected by default by specifying its status.
whiptail --title "" --radiolist "" [ ] . . .
Example:
点击(此处)折叠或打开
A checklist dialog is useful when you want to ask a user to choose more than one option among a list of options, which is in contrast to a radiolist box which allows only one selection.
whiptail --title "" --checklist "" [ ] . . .
Example:
点击(此处)折叠或打开
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. 一字之差.
下面有两个例子:
点击(此处)折叠或打开
点击(此处)折叠或打开
By now, you must see how easy it is to create useful dialog boxes in an 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? :-)