Shell script-case statement (including application cases)

case loop syntax structure

case 变量 in
	模式1)
		命令序列1
		;;
	模式2)
		命令序列2
		;;
	模式3)
		命令序列3
		;;
	*)
		无匹配后命令序列
	esac

Applications

delete users

#!/bin/bash
read -p "请输入用户名:" user

id $user &>/dev/null
if [ $? -ne 0 ];then
        echo "没有这个用户:$user"
        exit 1
fi

read -p "你确定吗?[y/n]:" action
case "$action" in
y|Y|yes|YES)
        userdel -r $user
        echo "$user 已删除!"
        ;;
*)
        echo "输入错误"
esac

System Toolbox

#!/bin/bash
#系统工具箱 
#by 张恒 2020-12-31
menu () {
    
    
cat <<-EOF
	
	 ##################################
	 #       h.帮助			          #
	 #       x.系统信息		          #
	 #       f.磁盘分区信息             #
	 #       d.系统文件挂载和使用情况     #
	 #       m.内存使用                #
	 #       u.系统负载                #
	 #       q.退出                   #
	 ##################################

	EOF
}
menu

xinxi() {
    
    
name=`hostname`
linux=`uname -r`
cpu=`cat /proc/cpuinfo |grep "model name" |awk -F ": " '{print $2}'`
mem=`cat /proc/meminfo |grep "MemTotal" |awk -F ": *" '{print $2}'`
disk=`fdisk -l |grep "sda:" |awk '{print $2}' |awk -F ":" '{print $2"GB"}'`
ip=`ifconfig ens33 |grep "netmask" |awk '{print $2}'`

echo "主机名:$name"
echo "内核版本:$linux"
echo "CPU型号:$cpu"
echo "内存大小:$mem"
echo "主硬盘大小:$disk"
echo "IP:$ip"
}

while :
do

read -p "请输入想使用的功能[h查看菜单]:" action
case $action in
h)
	clear
	menu
	;;
x)
	xinxi
	;;
f)
	fdisk -l
	;;
d)
	df -hT
	;;
m)
	free -h
	;;
u)
	iostat
	;;
q)
	break
	;;
"")
	;;
*)
	echo "错误!"

esac

done

system service

You can use scripts to control system services with one click

#!/bin/bash
#系统菜单
. /etc/init.d/functions
cat <<-EOF
        #########################
        #       程序菜单        #
        #       1.firewalld     #
        #       2.apche         #
        #       3.dhcp          #
        #       4.ftp           #
        #       5.dns           #
        #       6.docekr        #
        #       7.nginx         #
        #########################
        EOF

read -p "请输入你想执行的程序:" zhu 
case $zhu in
1)
        name=firewalld
        ;;
2)
        name=httpd
        ;;
3)
        name=dhcpd
        ;;
4)
        name=vsftpd
        ;;
5)
        name=named
        ;;
6)
        name=docker
        ;;
7)
        name=nginx
        ;;
*)
        echo "滚开!"
        ;;
esac

menu() {
    
    
cat <<-EOF
        #########################
        #       操作菜单        #
        #       1.启动          #
        #       2.重启          #
        #       3.重载          #
        #       4.查看状态      #
        #       5.停止          #
        #       6.永久关闭      #
        #       7.开机自启动    #
        #       h.菜单          #
        #       q.退出          #
        #########################

        EOF
}
menu

while :
do

read -p "请输入你想执行的动作(h返回菜单):" action
case $action in
1)
        systemctl start $name
        if [ $? -eq 0 ];then
                action "the $name is start" /bin/true
                echo "systemctl start $name"
        else
                action "the $name start is failed" /bin/false
                echo "systemctl start $name failed"
        fi
        ;;
2)
        systemctl restart $name
        ;;
3)
        systemctl reload $name
        ;;
4)
        systemctl status $name
        ;;
5)
        systemctl stop $name
        ;;
6)
        systemctl disable $name
        ;;
7)
        systemctl enable $name
        ;;
h)
        menu
        ;;
q)
        break
        ;;
*)
        echo "滚开!"
esac
done

Guess you like

Origin blog.csdn.net/Cantevenl/article/details/115304314