hylan:whiptail的用法

应用场景:当你在终端环境下安装新的软件时,你可能经常看到信息对话框弹出,需要你和它交互。对话框的类型有表单、密码框、菜单 等等。它们可以引导你以一种直观的方式输入必要的信息,使用这样用户友好的对话框的好处显而易见。

whiptail可以shell脚本中创建基于终端的对话框、消息框。预先安装在所有的Linux发布版本中。类似于Zenityxdialog GUI脚本代码。

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


whiptail使用中的提示:

Box options:
    --msgbox <text> <height> <width>
    --yesno  <text> <height> <width>
    --infobox <text> <height> <width>
    --inputbox <text> <height> <width> [init]
    --passwordbox <text> <height> <width> [init]
    --textbox <file> <height> <width>
    --menu <text> <height> <width> <listheight> [tag item] ...
    --checklist <text> <height> <width> <listheight> [tag item status]...
    --radiolist <text> <height> <width> <listheight> [tag item status]...
    --gauge <text> <height> <width> <percent>
Options: (depend on box-option)
    --clear                clear screen on exit
    --defaultno            default no button
    --default-item <string>        set default string
    --fb, --fullbuttons        use full buttons
    --nocancel            no cancel button
    --yes-button <text>        set text of yes button
    --no-button <text>        set text of no button
    --ok-button <text>        set text of ok button
    --cancel-button <text>        set text of cancel button
    --noitem            don't display items
    --notags            don't display tags
    --separate-output        output one line at a time
    --output-fd <fd>        output to fd, not stdout
    --title <title>            display title
    --backtitle <backtitle>        display backtitle
    --scrolltext            force vertical scrollbars
    --topleft            put window in top-left corner
    -h, --help            print this message
    -v, --version            print version information

man whiptail

SYNOPSIS(简介)whiptail  [  --title  title  ]  [ --backtitle backtitle ] [ --clear ] [
       --default-item string ] [ --defaultno ] [  --fb  ]  [  --nocancel  ]  [
       --yes-button  text ] [ --no-button text ] [ --ok-button text ] [ --can‐
       cel-button text ] [ --noitem [ ] --output-fd fd ] [ --separate-output ]

       [ --scrolltext ] [ --topleft ] box-options

BOX OPTIONS(框选项):

yesno选项框    --yesno text height width

提示框            --msgbox text height width

信息框            --infobox text height width

输入框            --inputbox text height width [init]

密码框            --passwordbox text height width [init]

文本框            --textbox file height width

菜单               --menu text height width menu-height [ tag item ] ...

清单               --checklist text height width list-height [ tag item status ] ...

选择列表        --radiolist text height width list-height  [ tag item status ] ...

测量               --gauge text height width percent


1.(yesno)创建一个Yes/No对话框

语法: whiptail --title "<dialog box title>" --yesno "<text to show>" <height> <width>

用户输入yes或no的对话框。

例如:
if ( whiptail -- title "Title hylan:yesno" -- 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

又如:

if (whiptail --title "Title hylan:yesno" --yes-button "Hylan" --no-button "Candy"  --yesno "Which do you like better?" 10 60) then    

echo "You chose Hylan. Exit status was $?."; 

echo "You Best!"

else    

echo "You chose Candy. Exit status was $?.";

fi

2.(msgbox)创建一个消息框

语法: whiptail --title "<message box title>" --msgbox "<text to show>" <height> <width>

一个消息框中显示一个确认按钮,继续任意的文本消息。

例如:

whiptail --title "Title hylan:msgbox" --msgbox "Create a message box with whiptail. Choose Ok to continue." 10 60

3.(inputbox)创建一个表单输入框

语法:whiptail --title "<input box title>" --inputbox "<text to show>" <height> <width> <default-text>

用户输入任意的文本您可以使用一个输入框

例如:PET=$(whiptail --title "Title hylan:inputbox" --inputbox "What is your pet's name?" 10 60 hylancandy 3>&1 1>&2 2>&3)

4.(passwordbox)创建一个密码框

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

例如:PASSWORD=$(whiptail --title "Title hylan:passwordbox" --passwordbox "Enter your password and choose Ok to continue." 10 60 3>&1 1>&2 2>&3)

5.(menu)创建一个菜单栏

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

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

例如:OPTION=$(whiptail --title "Title hylan:menu" --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)

6.(checklist)创建一个表对话框

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

例如:

DISTROS = $ ( whiptail -- title "Title hylan:checklist" -- 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

7.(radiolist)创建radiolist对话框

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

例如:DISTROS=$(whiptail --title "Title hylan:radiolist" --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)

8.(gauge)创建一个进度条

语法:whiptail --gauge "<test to show>" <height> <width> <inital percent>

例如:

{
     for ( ( i = 0 ; i <= 100 ; i += 20 ) ) ; do
         sleep 1
         echo $ i
     done
} | whiptail -- gauge "Please wait while installing" 6 60 0



猜你喜欢

转载自blog.csdn.net/t_thylan/article/details/79865280