如何在交互式shell脚本中创建对话框

默北 Shell1229,35833字数 3281阅读10分56秒阅读模式

当你在终端环境下安装新的软件时,你可以经常看到信息对话框弹出,需要你的输入。对话框的类型有密码箱,检查表,菜单,等等。他们可以引导你以一种直观的方式输入必要的信息,使用这样的用户友好的对话框的好处是显而易见的。如下图所示:

shell文章源自运维生存时间-https://www.ttlsa.com/shell/how-to-create-dialog-boxes-in-interactive-shell-script/

当你写一个交互式shell脚本,你可以使用这样的对话框来接受用户的输入。whiptail可以shell脚本中创建基于终端的对话框,消息框的过程,类似于Zenityxdialog GUI脚本代码。预先安装在所有的Linux发布版本中。文章源自运维生存时间-https://www.ttlsa.com/shell/how-to-create-dialog-boxes-in-interactive-shell-script/

下面来看看whiptail的用法:文章源自运维生存时间-https://www.ttlsa.com/shell/how-to-create-dialog-boxes-in-interactive-shell-script/

创建一个消息框

一个消息框中显示一个确认按钮继续任意的文本消息文章源自运维生存时间-https://www.ttlsa.com/shell/how-to-create-dialog-boxes-in-interactive-shell-script/

语法:文章源自运维生存时间-https://www.ttlsa.com/shell/how-to-create-dialog-boxes-in-interactive-shell-script/

whiptail --title "<message box title>" --msgbox "<text to show>" <height> <width>文章源自运维生存时间-https://www.ttlsa.com/shell/how-to-create-dialog-boxes-in-interactive-shell-script/

实例:文章源自运维生存时间-https://www.ttlsa.com/shell/how-to-create-dialog-boxes-in-interactive-shell-script/

#!/bin/bash
whiptail --title "Test Message Box" --msgbox "Create a message box with whiptail. Choose Ok to continue." 10 60

shell文章源自运维生存时间-https://www.ttlsa.com/shell/how-to-create-dialog-boxes-in-interactive-shell-script/

创建一个yes/no对话框

用户输入yes或no的对话框。文章源自运维生存时间-https://www.ttlsa.com/shell/how-to-create-dialog-boxes-in-interactive-shell-script/

语法:文章源自运维生存时间-https://www.ttlsa.com/shell/how-to-create-dialog-boxes-in-interactive-shell-script/

whiptail --title "<dialog box title>" --yesno "<text to show>" <height> <width>文章源自运维生存时间-https://www.ttlsa.com/shell/how-to-create-dialog-boxes-in-interactive-shell-script/

实例:文章源自运维生存时间-https://www.ttlsa.com/shell/how-to-create-dialog-boxes-in-interactive-shell-script/

#!/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

shell文章源自运维生存时间-https://www.ttlsa.com/shell/how-to-create-dialog-boxes-in-interactive-shell-script/

或者,你可以是“--yes-button” ,"--no-button"选项。文章源自运维生存时间-https://www.ttlsa.com/shell/how-to-create-dialog-boxes-in-interactive-shell-script/

#!/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

shell文章源自运维生存时间-https://www.ttlsa.com/shell/how-to-create-dialog-boxes-in-interactive-shell-script/

创建一个表单输入

如果你想用户输入任意的文本您可以使用一个输入框文章源自运维生存时间-https://www.ttlsa.com/shell/how-to-create-dialog-boxes-in-interactive-shell-script/

语法:文章源自运维生存时间-https://www.ttlsa.com/shell/how-to-create-dialog-boxes-in-interactive-shell-script/

whiptail --title "<input box title>" --inputbox "<text to show>" <height> <width> <default-text>文章源自运维生存时间-https://www.ttlsa.com/shell/how-to-create-dialog-boxes-in-interactive-shell-script/

实例:文章源自运维生存时间-https://www.ttlsa.com/shell/how-to-create-dialog-boxes-in-interactive-shell-script/

#!/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

shell文章源自运维生存时间-https://www.ttlsa.com/shell/how-to-create-dialog-boxes-in-interactive-shell-script/

创建一个密码框

用户需要输入敏感信息时密码框是有用的

语法:

whiptail --title "<password box title>" --passwordbox "<text to show>" <height> <width>

实例:

#!/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

shell

创建一个菜单栏

当你想让用户选择一个任意数量的选择中,你可以使用菜单框

语法:

whiptail --title "<menu title>" --menu "<text to show>" <height> <width> <menu height> [ <tag> <item> ] . . .

实例:

#!/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

shell

创建radiolist对话框

语法:

whiptail --title "<radiolist title>" --radiolist "<text to show>" <height> <width> <list height> [ <tag> <item> <status> ] . . .

实例:

#!/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

shell

创建一个对话框

当你想让用户选择一个列表中选择多个选项的清单对话框是有用的,radiolist对话框,只允许选择一个

语法:

whiptail --title "<checklist title>" --checklist "<text to show>" <height> <width> <list height> [ <tag> <item> <status> ] . . .

实例:

#!/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

shell

创建一个进度条

进度条是一个用户友好的对话框。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

shell

哈哈,是多么容易在交互式shell脚本创建有用的对话框了吧。下次需要写一个交互式的shell脚本,试着用whiptail哈。

weinxin
我的微信
微信公众号
扫一扫关注运维生存时间公众号,获取最新技术文章~
默北
  • 本文由 发表于 15/12/2014 01:00:33
  • 转载请务必保留本文链接:https://www.ttlsa.com/shell/how-to-create-dialog-boxes-in-interactive-shell-script/
  • shell
  • whiptail
  • xdialog GUI
  • Zenity
评论  12  访客  12
    • ysicing
      ysicing 1

      Get。赶紧试了试,不错哟

      • 苏幕遮
        苏幕遮 1

        这技术碉堡了啊!!!

        • freshpassport
          freshpassport 0

          不错,非常好。

          • 萌新求指导
            萌新求指导 9

            楼主,创建radiolist对话框这个,如何切换选项呢,只能通过脚本的”debian” “Venerable Debian” ON 中,ON或者OFF来控制吗? 萌新希望大牛指导(email:3245225445#qq.com)

            • 铁板烧
              铁板烧 9

              恩 不错

              • leoiceo
                leoiceo 0

                必须赞,转载一下

                • sunboy
                  sunboy 0

                  • swordman
                    swordman 9

                    狂赞

                    • 尛丶鑫鑫
                      尛丶鑫鑫 9

                      谢谢分享~又学到了好多新的东西

                      • 运维生存时间
                        运维生存时间 7

                        Good

                        • 赞
                          9

                          怒赞

                          • 锟斤拷
                            锟斤拷 9

                          评论已关闭!