《Linux就该这么学》是由全国多名红帽架构师(RHCA)基于最新Linux系统共同编写的高质量Linux技术自学教程,极其适合用于Linux技术入门教程或讲课辅助教材。
分类: LINUX
2016-04-11 08:52:25
当你在终端环境下安装新的软件时,你可以经常看到信息对话框弹出,需要你的输入,比如:RHEL/CentOS自带的setup,对话框的类型有密码箱、检查表、菜单等等。他们可以引导你以一种直观的方式输入必要的信息,使用这样的用户友好的对话框的好处是显而易见的。如下图所示:
当你写一个交互式shell脚本,你可以使用这样的对话框来接受用户的输入。whiptail可以在shell脚本中创建基于终端的对话框,消息框的过程,类似于Zenity或xdialog GUI脚本代码。whiptail预先安装在所有的Linux发布版本中。
语法:
whiptail --title "" --msgbox ""
实例:
#!/bin/bash whiptail --title "Test Message Box" --msgbox "Create a message box with whiptail. Choose Ok to continue." 10 60
语法:
whiptail --title "" --yesno ""
实例:
#!/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
或者,你可以是“--yes-button” ,"--no-button"选项。
#!/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
语法:
whiptail --title "" --inputbox ""
实例:
#!/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
语法:
whiptail --title "" --passwordbox ""
实例:
#!/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
语法:
whiptail --title " " --menu "" [ ] . . .
实例:
#!/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
语法:
whiptail --title "" --radiolist "" [ ] . . .
实例:
#!/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
语法:
whiptail --title "" --checklist "" [ ] . . .
实例:
#!/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
语法:
whiptail --gauge ""
实例:
#!/bin/bash { for ((i = 0 ; i <= 100 ; i+=20)); do sleep 1 echo $i done } | whiptail --gauge "Please wait while installing" 6 60 0
本文转载自:
免费提供最新Linux技术教程书籍,为开源技术爱好者努力做得更多更好: