How to use whiptail to write interactive shell scripts
来源:互联网 发布:波士顿房价数据集 编辑:程序博客网 时间:2024/05/21 15:49
FROM:
http://www.techrepublic.com/blog/linux-and-open-source/how-to-use-whiptail-to-write-interactive-shell-scripts/
Marco Fioretti shows you how to use whiptail, a program that lets you create a variety of dialog boxes from a shell script.
Many novice Linux users would have many less problems in using the command line if, in any moment, they only had to make choices within a limited,predefined set of alternatives offered by the script they are using.
A way to quickly write such scripts is to use whiptail. As you can see in Figure A, whiptail produces the same kind of dialogs and menus normally found in the text installation procedures of many Linux distributions. Besides running entirely inside a terminal, whiptail has a fewless features than Zenity. The two most important missing widgets are the graphical file selector and the calendar. Whiptail, however, has all the other, most common interfaces you may need in a shell script. The man page of whiptail is quite complete except, in my opinion, for one gap that I will try to fill here. Namely, the whiptail documentation describes all the available widgets and all their options, but it is too synthetic when it comes to where the output of whiptail goes, and how to retrieve it.
Figure A
Almost all whiptail functions generate a binary output, to tell the calling script which of two buttons (usually "OK" and "Cancel") the user pressed. Whiptail always encodes this information in its exit status, which is available in the special shell variable $?. As an example, the widget in Figure A comes from this piece of code:
#! /bin/bash
whiptail --yesno "Did you already know whiptail?" --yes-button "Yes, I did" --no-button "No, never heard of it" 10 70
CHOICEs=$?
When you run that code, $CHOICE becomes 0 when you answer ,"Yes, I did", 1 if you press "No..." and -1 if some error occurred.
As I said, this is the base behavior of whiptail. Several functions, however, must also pass to the script sometext that the user typed or selected. The man page does say that such text is written to the standard error, but stops there, without describing how to retrieve it in practice. It is possible, as explained inthis example, to directly save that text in a shell variable. Personally, I very much prefer to write everything to a temporary file: this approach leaves traces of how the script worked in case of problems or crashes and makes it easier both to parse the output and to understand the code. To see what I mean and how to do it, please look at the whiptail command that generates the menu of Figure B:
whiptail --menu "Preferred Linux Distro" 10 40 4 \
debian "Venerable Debian" off \
ubuntu "Popular Ubuntu" on \
fedora "Hackish Fedora" off \
centos "Stable Centos" off \
mint "Rising Star Mint" off 2>distrochoice
Figure B
The trick is in the last line. In Unix, the numeric descriptor for the standard error of a program has 2 as default value 2. Therefore, adding2>distrochoice to the whiptail invocation redirects its standard error to the distrochoice file. That is where you will find the tag (debian in the case of Figure B) corresponding to the selected voice, while $? will still work as I already explained.
Should you need to retrieve multiple choices, as shown in Figure C, you should use the --checklist function:whiptail --checklist "Preferred Linux distros" 15 40 5\
debian "Venerable Debian" off \
ubuntu "Popular Ubuntu" on \
fedora "Hackish Fedora" off \
centos "Stable Centos" off \
mint "Rising Star Mint" off 2>distrochoice
Figure C
- How to use whiptail to write interactive shell scripts
- how to write shell scripts
- How to write shell script
- How to use boost::property_tree to load and write JSON
- How to write a simple shell program
- how to use the .htaccess shell attack
- [bash] how to use bash-shell array
- How To: Interactive Kiosks-Part1
- How To: Interactive Kiosks-Part2
- How to use java Properties API -- read and write
- How to Package Python Scripts
- How to add custom scripts
- How to write Bibliography
- how to write makefile
- How to write makefile
- How to write patent
- how to write Makefile
- DLL - How to Write
- Populating Next Right Pointers in Each Node II
- hdu 1227(经典dp)
- 打开Office2007版Word时,老是出现一个“配置进度”的窗口说“正在配置Microsoft Office Professional Plus
- nm使用记录
- HDU 2112 HDU Today 最短路问题map+floyd
- How to use whiptail to write interactive shell scripts
- 黑马程序员_iOS开发C语言基础之运算符和流程控制
- Android获取文件目录路径
- Linux运维好书《高性能Linux服务器构建实战Ⅱ》已出版发售,附封面照!
- 44 腾讯面试题 魔方 短信 url
- typedef
- 基于XMPP协议的Android即时通信
- 45 雅虎判断其是否能够由一个全零矩阵经过上述运算得到
- 45 整数数组,将其分为m份使各份的和相等