S02.shell圖形化程式設計
阿新 • • 發佈:2022-12-12
whiptail使用
訊息框語法:
whiptail --title "<message box title>" --msgbox "<text to show>" <height> <width>
示例:
#!/bin/bash
#--title:表示指定標題內容
#--msgbox:執行資訊內容
#20表示訊息框高度為20
#60表示訊息狂的寬度為60
whiptail --title "$HOSTNAME whiptail test" --msgbox "`df -TH`" 20 60
布林值選擇框
語法:whiptail --title "<dialog box title>" --yesno "<text to show>" <height> <width>
示例1:
#!/bin/bash
if (whiptail --title "TEST Yes/No Box" --yesno "請選擇yes/no" 10 60)
then
echo "您選擇的為yes,exit status was $?"
else
echo "您選擇的為No,exit status was $?"
fi
示例2:
#!/bin/bash if (whiptail --title "TEST Yes/No Box" --yesno "請選擇yes/no" 10 60) then echo "您選擇的為yes,exit status was $?" else echo "您選擇的為No,exit status was $?" fi
互動式輸入框
語法:
whiptail --title "標題" --inputbox "資訊" <高度> <寬度> <預設值>
示例
#!/bin/bash
ip=`whiptail --title "互動式輸入框" --inputbox "請輸入您vip資訊" 10 60 192.168.40.100 3>&1 1>&2 2>&1`
if [ $? -eq 0 ];then
echo "$ip"
else
echo "輸入ip錯誤"
fi
密碼框:
語法
whiptail --title "<password box title>" --passwordbox "<text to show>" <height> <width>
示例
#!/bin/bash
password=$(whiptail --title "密碼框" --passwordbox "輸入的密碼" 10 60 3>&1 1>&2 2>&1)
if [ $? -eq 0 ]
then
echo "輸入密碼正確"
else
echo "輸入密碼錯誤"
fi
選單欄
語法
whiptail --title "<menu title>" --menu "<text to show>" <height> <width> <menu height> [ <tag> <item> ] . .
示例:
--menu # 選單欄
30 # 高度
60 # 寬度
3 # 選單顯示多少行內容
#!/bin/bash
a=$(whiptail --title "選單欄" --menu "根據選單選" 30 60 3 \
"1" "增加" \
"2" "刪除" \
"3" "擴容" \
"4" "縮容" \
"5" "lb05" \
"6" "lb06" 3>&1 1>&2 2>&3)
單選框
# 語法
a=$(whiptail --title "家裡" --radiolist "根據選單選" 30 60 3 \
"1" "lb01" OFF \
"2" "lb2" ON \
"3" "lb03" OFF \
"4" "lb04" OFF \
"5" "lb05" OFF \
"6" "lb06" OFF 3>&1 1>&2 2>&3)
# 選項
--rediolist : 單選框
OFF : 預設沒有被選中
ON :預設被選中
多選沒有意義,後面的值會覆蓋前面的選項
# 返回值
OK :0
Cancel :1
多選框:
a=$(whiptail --title "家裡" --checklist "根據選單選" 30 60 3 \
"1" "lb01" OFF \
"2" "lb2" ON \
"3" "lb03" OFF \
"4" "lb04" OFF \
"5" "lb05" OFF \
"6" "lb06" OFF 3>&1 1>&2 2>&3)
進度條:
{
for ((i = 0 ; i <= 100 ; i+=1));do
sleep 10
echo $i
done
} | whiptail --gauge "讓子彈飛一會" 6 60 10