Linux里命令的对话框whiptail


Linux里面可以在命令行里形成对话框,用光标上下,左右选择,一屏一屏的问答选择,填写。
whiptail是Linux不需要另行安装,默认就有的,其它的比如dialog需要另行安装,很麻烦,虽然功能比较好些。

帖一个写好的东西。
#!/bin/bash
trap "" 2
while true
do
OPTION=$(whiptail --title "Email Manager" --nocancel --menu "Choose your option" 15 60 4 \
"1" "Add Email User" \
"2" "Delete Email User" \
"3" "List Email User" \
"4" "EXIT"  3>&1 1>&2 2>&3)
    case $OPTION in
    1)
        EmailAddress=$(whiptail --title "EmailAddress-form Input Box" --inputbox "What is your add EmailAddress?" 10 60 @shenxu.com 3>&1 1>&2 2>&3)
        exitstatus=$?
        if [ $exitstatus = 0 ]; then
          grep $EmailAddress /etc/postfix/virtual_mailbox_maps>/dev/nul
          exitstatus=$?
            if [ $exitstatus = 0 ]; then
                whiptail --msgbox "The Email Address is a existed" 10 40
            elif (whiptail --title "Add Yes/No Box" --yesno "Are you sure add $EmailAddress." 10 60) then
              /etc/postfix/mailadd.sh $EmailAddress
              whiptail --msgbox "The Email Address $EmailAddress is a added." 10 40
            fi
        else
          whiptail --msgbox "You chose Cancel." 10 40
        fi
      ;;
    2)
        EmailAddress=$(whiptail --title "EmailAddress-form Input Box" --inputbox "What is your Delete EmailAddress?" 10 60 @shenxu.com 3>&1 1>&2 2>&3)
        exitstatus=$?
        if [ $exitstatus = 0 ]; then
          grep $EmailAddress /etc/postfix/virtual_mailbox_maps>/dev/nul
          exitstatus=$?
            if [ $exitstatus != 0 ]; then
                whiptail --msgbox "The Email Address $EmailAddress is a not exist." 10 40
            elif (whiptail --title "Add Yes/No Box" --yesno "Are you sure delete $EmailAddress." 10 60) then
              /etc/postfix/maildel.sh $EmailAddress
              whiptail --msgbox "The Email Address $EmailAddress is a deleted." 10 40
            fi
        else
          whiptail --msgbox "You chose Cancel." 10 40
        fi
      ;;
    3)
        EmailAddress=$(cat /etc/postfix/virtual_mailbox_maps | awk '{print $1}')
        whiptail --msgbox "The Email User list are $EmailAddress." --scrolltext 20 40
      ;;
    4)
        echo "EXIT"
        break
      ;;
  esac
done
trap : 2

 


whiptail --title "Email Manager" --nocancel --menu "Choose your option" 15 60 4 \
"1" "Add Email User" \
"2" "Delete Email User" \
"3" "List Email User" \
"4" "EXIT"  3>&1 1>&2 2>&3
--title "Email Manager" 是标题,双引号里是自己填的提示信息
--nocancel 是在这个图文里面不显示取消,只显示OK
--menu "Choose your option" 15 60 4 是表示菜单提示,双引号里是自己填的提示信息,15是高度,60是长度,4是有个选择项目
下面的1-4是自己的提示
最后比较关键,3>&1 1>&2 2>&3是为了把选择的内容填进变量OPTION

whiptail --title "EmailAddress-form Input Box" --inputbox "What is your add EmailAddress?" 10 60 @shenxu.com 3>&1 1>&2 2>&3
--inputbox "What is your add EmailAddress?" 是可以形成一个让用户输入的提示框
@shenxu.com 是默认输入text里的值

whiptail --msgbox "You chose Cancel." 10 40 是显示一行你的提示
其实还有--infobox,似乎和msgbox很像,其实不同,它基本上用不上,是在shell运行完后,可以往前翻页能看见的东西
--scrolltext 20 40是为了显示多行的时候可以上下滚动

另外还有--passwordbox和text一样输入,就是以***显示
whiptail --checklist "choose" 15 60 2 "1" "aa" ON "2" "bb" ON
15 60还是高和宽,2是有几个选项,和menu一样,后面多了一个ON或者OFF表示状态,就是菜单出来后默认是不是选,On是选,OFF不选,用空格键来选择。可以多选。
--radiolist,不可以多选了。ON就只能有一个,其它必须是OFF
还有一个显示进度条的--gauge,我觉得没啥用处。
#!/bin/bash
 {
    for n in `seq 100`
    do
        sleep 1
    echo $n
    done
 } | whiptail --gauge "Please wait while installing" 6 60 0

本文永久更新链接地址

相关内容