经常看见在配置内核,配置根文件系统的时候看到这种菜单,还有Centos、中标麒麟自带的setup,突然很想自己也做一个,还有就是为了测试调用命令,感觉挺好的
查了一些资料,有几个可以写shell界面,whiptail,Zenity或者xdialog.,我看了一下,在ubuntu16.04中,有两个是自带的,一个是whiptail和Zenity,而xdialog是需要安装的。
现在从whiptail的开始
1、创建一个消息框
一个消息框中显示一个确认按钮继续任意的文本消息。
语法:
whiptail --title "<message box title>" --msgbox "<text to show>" <height> <width>
#!/bin/sh
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
2、创建一个yes/no对话框
用户输入yes或no的对话框。
语法:
whiptail --title "<dialog box title>" --yesno "<text to show>" <height> <width>
实例:
#!/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/sh
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
3、创建一个表单输入框
如果你想用户输入任意的文本,您可以使用一个输入框。
语法:
whiptail --title "<input box title>" --inputbox "<text to show>" <height> <width> <default-text>
实例:
#!/bin/sh
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
4、创建一个密码框
当用户需要输入敏感信息时密码框是有用的。
语法:
whiptail --title "<password box title>" --passwordbox "<text to show>" <height> <width>
实例:
#!/bin/sh
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
5、创建一个菜单栏
当你想让用户选择一个任意数量的选择中,你可以使用菜单框。
语法:
whiptail --title "<menu title>" --menu "<text to show>" <height> <width> <menu height> [ <tag> <item> ] . . .
实例:
#!/bin/sh
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
6、创建radiolist对话框
语法:
whiptail --title "<radiolist title>" --radiolist "<text to show>" <height> <width> <list height> [ <tag> <item> <status> ] . . .
实例:
#!/bin/sh
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
7、创建一个表对话框
当你想让用户选择一个列表中选择多个选项的清单对话框是有用的,radiolist对话框,只允许选择一个。
语法:
whiptail --title "<checklist title>" --checklist "<text to show>" <height> <width> <list height> [ <tag> <item> <status> ] . . .
实例:
#!/bin/sh
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
8、创建一个进度条(这个需要在bash运行,上面的是sh)
进度条是一个用户友好的对话框。whiptail从标准输入读取一个百分数(0~100),显示一个表内相应的计数。
语法:
whiptail --gauge "<test to show>" <height> <width> <inital percent>
实例:
#!/bin/bash
{
for ((i = 0 ; i <= 100 ; i+=20)); do
sleep 1
echo $i
done
} | whiptail --gauge "Please wait while installing" 6 60 0