S02.shell图形化编程

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

参考链接:https://www.cnblogs.com/ghjhkj/p/16526022.html

posted @ 2022-12-12 16:50  Mr-呵呵哒  阅读(59)  评论(0编辑  收藏  举报