交互式shell腳本對話框----whiptail指令


當你在linux環境下setup軟件的時候就會有相應的對話框讓你輸入。雖然我們已經習慣了這種交互的方法,但是如果有一種直觀的界面來輸入是不是會更加友好和方便呢,在shell腳本中你可以使用-whiptail指令來完成。

 

消息框

語法:

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

實例:

whiptail --title "Message box title" --msgbox " Choose Ok to continue." 10 60

 yes/no對話框

語法:

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

實例:

#!/bin/bash
if (whiptail --title "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

或者也可以是自定義的選項,實例如下:

#!/bin/bash
if (whiptail --title "Yes/No Box" --yes-button "Man" --no-button "Woman"  --yesno "What is your gender?" 10 60) then
    echo "You chose Man Exit status was $?."
else
    echo "You chose Woman. Exit status was $?."
fi

當選擇左邊選項的時候輸出的是0,選擇右邊選項的時候輸出的是1。

表單輸入框

語法:

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

實例:

#!/bin/bash
NAME=$(whiptail --title "Free-form Input Box" --inputbox "What is your pet's name?" 10 60 Peter 3>&1 1>&2 2>&3)

exitstatus=$?
if [ $exitstatus = 0 ]; then
    echo "Your name is:" $NAME
else
    echo "You chose Cancel."
fi

 密碼輸入框

 

語法:

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

實例:

#!/bin/bash
PASSWORD=$(whiptail --title "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 title>" --menu "<text to show>" <height> <width> <menu height> [ <tag> <item> ] . . .

實例:

#!/bin/bash
OPTION=$(whiptail --title "Menu Dialog" --menu "Choose your favorite programming language." 15 60 4 \
"1" "Python" \
"2" "Java" \
"3" "C" \
"4" "PHP"  3>&1 1>&2 2>&3)

exitstatus=$?
if [ $exitstatus = 0 ]; then
    echo "Your favorite programming language is:" $OPTION
else
    echo "You chose Cancel."
fi

此處的選擇輸出的內容為你選擇的標簽的‘tag’位置,上面實例中的‘1’、‘2’、‘3’、‘4’

 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  

多選對話框

 

語法:

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

實例:

#!/bin/bash
DISTROS=$(whiptail --title "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 "<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

  

 

參考鏈接:https://www.cnblogs.com/probemark/p/5890599.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM