在CentOS6系统中,我们可以使用 setup 指令来修改网卡的IP等信息,交互起来十分方便
在CentOS7系统中, setup 命令已经没有了,但是还有 nmtui 命令,可以让我们修改IP和主机名。
那么 whiptail 命令的作用,就是出现一个可以交互的图形化界面,并且样式有很多。在之前的课程中,我们已经使用流程控制语句,满足了一个跳板机的需求,但是我们还想是想更多的功能,当然脚本也都能实现,但是我们想要更炫酷的,脱离死气沉沉的命令行。
那么就一起来看一下,whiptail可以实现哪些需求吧

信息框

#语法
whiptail --title "<标题内容>" --msgbox "<信息内容>" <height> <width>
#示例
whiptail --title "跳板机 $HOSTNAME Disk Info" --msgbox "`df -h`" 30 80
whiptail 就是出现一个可以交互的图形化界面
--title # 指定标题的内容 $HOSTNAME Disk Info
--msgbox # 指定信息内容 `df -h`
30 # 展示信息的高度为30
60 # 展示信息的宽度为60

image-20230704141335594

返回值

vim 1.sh
#!/bin/bash
# 信息框
whiptail --title "跳板机 $HOSTNAME Disk Info" --msgbox "`echo 123" 10 60
echo $?
------------------------------------------------------------------------------------------------------
[root@m01 ~]# sh 1.sh    //执行完脚本后会返回一个值
0

image-20230704141604691

布尔值的选择框

#语法
whiptail --title "<dialog box title>" --yesno "<text to show>" <height> <width>
#示例
whiptail --title "跳板机 $HOSTNAME Disk Info" --yesno '请输入你的选项 YES or NO !' 10 60

image-20230704141817306

#自定义YES/NO
#语法
whiptail --title "<dialog box title>" --yes-button "true" --no-button "false"  --yesno "<text to show>" 10 60

--title # 标题
--yes-button # yes的按钮可以改名
--no-button # no的按钮可以改名
--yesno # 布尔值框 后面可以追加框内的内容

image-20230704142410853

交互式输入框

#语法
原:whiptail --title "<input box title>" --inputbox "<text to show>" <height> <width> <default-text>
译:whiptail --tile <标题> --inputbox "<信息>" 高度 宽度 默认值
#示例
whiptail --title "<input box title>" --inputbox "<text to show>" 10 60 2

image-20230704142732441

#案例:简单的远程传输文件
source_file=`whiptail --title "jump ji" --inputbox "请输入一个文件路径" 20 50 /etc/passwd 3>&1 1>&2 2>&3`
if [ $? -eq 0 ];then
dest_file=`whiptail --title "jump ji" --inputbox "请输入一个对端存放路径" 20 50 /opt 3>&1 1>&2 2>&3`
    if [ $? -eq 0 ];then
        scp $source_file 172.16.1.7:$dest_file &>/dev/null
    else
        echo '请输入一个目标路径'
    fi
else
    echo '请输入一个源文件路径'
fi

密码输入框

#语法
原:whiptail --title "<password box title>" --passwordbox "<text to show>" <height> <width>
#示例
passwd=`whiptail --title 'jump' --passwordbox "请输入一个密码:" 10 60 3>&1 1>&2 2>&3`

image-20230704143311914

#案例模拟
passwd=`whiptail --title 'jump' --passwordbox "请输入一个密码:" 10 60 3>&1 1>&2 2>&3`
if [ $? -eq 0 ];then
        if [ ${#passwd} -ne 0 ];then
                echo "密码是 $passwd"
        else
                echo '密码为空'
        fi
else
        echo '选择了取消'
fi

菜单栏

#语法一  //把执行结果定义的一个变量里,后续只要使用调用这个变量即可
OPTION=$(whiptail --title "Menu Dialog" --menu "Choose your favorite programming language." 15 60 4 \
"选项一" \
"选项二" \
"选项三" \
"4" "PHP"  3>&1 1>&2 2>&3
#语法二
whiptail --tile <标题> --menu "<信息>" 高度宽度 页面显示个数
whiptail --title "jump" --menu "根据菜单选择" 20 30 4 \
'1' lb01 \
'2' lb02 \
'3' web01 \
'4' web02 \
'5' web03 3>&1 1>&2 2>&3

image-20230704143933896

单选框(radiolist

该对话框是单选对话框,你可以控制默认的选择位置,即使你在脚本中默认选择多个,他也只会输出一个结果

#语法
whiptail --title "<标题栏>"  --radiolist <text> <height> <width> <listheight> [tag item status]...
whiptail --title "jump" --radiolist "信息" 高度 宽度 页面显示个数
#示例
res=`whiptail --title "jump" --radiolist "请在下面选择一项" 10 60 4 \
"send" "发送文件" OFF \
"useradd" "创建用户" OFF \
"ssh" "远程连接" OFF \
"mem" "查看内存" OFF 3>&1 1>&2 2>&3`
echo $res

image-20230704144301233

#综上方式执行,那么在输出结果的时候会把选项的双引号也一起打印出来
#可以使用多种方式使之其结果不带引号
#方法一:使用for循环
for n in $res;do
    if [ ${n//\"/} == "send" ];then
        echo '发送文件'
    elif [ ${n//\"/} == "useradd" ];then
        echo '创建用户'
    elif [ ${n//\"/} == "ssh" ];then
        echo '远程连接'
    elif [ ${n//\"/} == "mem" ];then
        echo '查看内存'
    fi
done
#方法二
echo $res 改为echo "${res//\"}" 
`"${res//\"}"` 是一个字符串操作的表达式,用来处理变量 `res` 中的双引号。
`${res//\"}` 是一种字符串替换的语法,它将 `res` 中的所有双引号 `"` 替换为空字符。
其中,`//\"` 中的两个斜杠表示替换所有匹配项,`\"` 表示要查找和替换的目标字符串,即双引号 `"`。
#方法三
echo $res | tr -d '"'

多选框/复选框

# 语法
whiptail --title "jump" --checklist "信息" 高度 宽度 页面显示个数

进度条

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

image-20230704145311122

#结合for循环使用
{
for ((i = 0 ; i <= 100 ; i+=1)); do
    sleep 0.09
    echo $i
done
} | whiptail --gauge "等一下子,正在安装" 6 60 0