詳解Linux交互式shell腳本中創建對話框實例教程_linux服務器


本教程我們通過實現來講講Linux交互式shell腳本中創建各種各樣對話框,對話框在Linux中可以友好的提示操作者,感興趣的朋友可以參考學習一下。

當你在終端環境下安裝新的軟件時,你可以經常看到信息對話框彈出,需要你的輸入。對話框的類型有密碼箱,檢查表,菜單,等等。他們可以引導你以一種直觀的方式輸入必要的信息,使用這樣的用戶友好的對話框的好處是顯而易見的。如下圖所示:

shell

當你寫一個交互式shell腳本,你可以使用這樣的對話框來接受用戶的輸入。whiptail可以在shell腳本中創建基於終端的對話框,消息框的過程,類似於Zenity或xdialog GUI腳本代碼。預先安裝在所有的Linux發布版本中。

下面來看看whiptail的用法:

創建一個消息框

一個消息框中顯示一個確認按鈕繼續任意的文本消息。

語法:

whiptail –title “” –msgbox “”

例:

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

shell

創建一個yes/no對話框

用戶輸入yes或no的對話框。

語法:

whiptail –title “” –yesno “”

實例:

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

或者,你可以是“–yes-button” ,”–no-button”選項。

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

創建一個表單輸入框

如果你想用戶輸入任意的文本,您可以使用一個輸入框。

語法:

whiptail –title “” –inputbox “”

實例:

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

創建一個密碼框

當用戶需要輸入敏感信息時密碼框是有用的。

語法:

whiptail –title “” –passwordbox “”

實例:

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

[ ] . . .

實例:

#!/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 “” [ ] . . .

實例:

#!/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 “” [ ] . . .

實例:

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

實例:

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

本文來源 :詳解Linux交互式shell腳本中創建對話框實例教程_linux服務器


免責聲明!

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



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