[Advanced operation and maintenance knowledge] Super detailed explanation of Shell programming 3 (if judgment + Shell menu + case process judgment + batch creation and deletion of users + guessing number game)

This article continues to introduce Shell programming to you, and take you to appreciate the charm of Shell. It is introduced by if judgment, so as to expand the Shell menu and case process judgment. There are also many cases prepared, including guessing games, creating users in batches, etc. Learning from cases will get twice the result with half the effort!

Table of contents

if judgment

1. Install different yum warehouses according to the version of the operating system judged

Shell menu

1. Install different software service versions

case process judgment

1. Use the menu to view memory, disk, load, and public IP

Two, jumperserver springboard machine

3. Use case to implement Nginx startup script

Combat practice

1. Create and delete users in batches

2. Guess the number game


if judgment

第一种写法,then在表达式的后面,需要在表达式后面加分号
if [表达式成立];then
  执行的具体命令集合
fi

第二种写法,then在if下面,表达式后面不需要加分号
if [表达式成立]
then
  执行的具体命令集合
fi

语句1:

if [ 你有钱 ]
then
  我就嫁给你
fi

一个条件一个结果,成立则执行then后面的动作,不成立则无视
类似 [ 表达式成立 ] && echo 成立

语句2:

if [ 你有钱 ]
then
  我就嫁给你
else
  拜拜
fi

一个条件两个结果,成立则执行then后面的动作,不成立则执行else后面内容
类似 [ 表达式成立 ] && echo 成立 || echo 不成立

语句3:

if [ 你有钱 ]
then
  我就嫁给你
elif [ 你有房 ]
then
  我委屈点嫁给你
elif [ 你长得帅 ]       
then
  我们先做朋友
else
  啥也不是
fi

多个条件多个结果,成立则执行then后面的动作,不成立则执行else后面内容

1. Install different yum warehouses according to the version of the operating system judged

[root@LB00 Day03]# cat os_install.sh
#!/bin/bash
#1、取系统版本号
os_ve=`hostnamectl|grep System|awk '{print $5}'`
mv_yum='mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup'
#2、判断网络是否正常
ping -c1 -W1 www.baidu.com &> /dev/null
if [ $? -ne 0 ];then
    echo "网络检查失败,正在重启网络..."
    systemctl restart network
    ping -c1 -W1 www.baidu.com &> /dev/null
    if [ $? -ne 0 ];then
        echo "网络重启完成,无法联网,请运维检查网络..."
        echo "网络重启完成,无法联网,请运维检查网络..." > /tmp/os_install.log
        mail -s '检查网络' [email protected] < /tmp/os_install.log
        exit
    fi
fi
#3、判断系统是否有安装wget命令
rpm -qa wget &> /dev/null
if [ $? -ne 0 ]
then 
    echo "wget命令未安装,正在安装中请稍后......"
    yum -y install wget &> /dev/null
    if [ $? -eq 0 ];then
        echo "wget安装成功..."
    else
        echo "wget安装失败,请手动测试..."
    fi
fi
#4、对比版本执行安装yum仓库
$mv_yum
if [ $os_ve -eq 7 ];then
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo &> /dev/null
    if [ $? -eq 0 ];then
        echo "yum仓库配置完成"
    fi
elif [ $os_ve -eq 6 ];then
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-6.10.repo &> /dev/null
    if [ $? -eq 0 ];then
        echo "yum仓库配置完成"
    fi
elif [ $os_ve -eq 8 ];then
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo &> /dev/null
    if [ $? -eq 0 ];then
        echo "yum仓库配置完成"
    fi
else
    echo 这个版本的系统没有准备yum仓库的安装。
fi
[root@LB00 Day03]# sh os_install.sh
yum仓库配置完成
[root@LB00 Day03]# ip route del default
[root@LB00 Day03]# ip route add default via 10.0.0.3 dev eth0
[root@LB00 Day03]# sh os_install.sh
网络检查失败,正在重启网络...
yum仓库配置完成

Shell menu

Two ways to create a menu, add color to the menu

[root@LB00 Day03]# cat menu.sh
#!/bin/bash
cat<<EOF
			1.PHP5.4
			2.PHP5.5
			3.PHP7.1
EOF
echo -e "\t\t\t\033[31m 1.PHP5.4 \033[0m"
echo -e "\t\t\t\033[32m 2.PHP5.5 \033[0m"
echo -e "\t\t\t\033[33m 3.PHP7.1 \033[0m"
[root@LB00 Day03]# sh menu.sh
			1.PHP5.4
			2.PHP5.5
			3.PHP7.1
			 1.PHP5.4 
			 2.PHP5.5 
			 3.PHP7.1 

eca1e776119e4f6cb6724b950b7abd39.png

Use read to interact with menus

[root@LB00 Day03]# cat menu.sh
#!/bin/bash1
while true                    #死循环
do

echo -e "\t\t\t\033[31m 1.PHP5.4 \033[0m"
echo -e "\t\t\t\033[32m 2.PHP5.5 \033[0m"
echo -e "\t\t\t\033[33m 3.PHP7.1 \033[0m"
echo -e "\t\t\t\033[33m 4.exit \033[0m"
read -p "请输入你要安装的版本的编号: " num
if [ $num == 1 -o $num == "PHP5.4" ]
then
    echo "配置PHP5.4yum仓库..."
    echo "安装PHP5.4..."
elif [ $num == 2 -o $num == "PHP5.5" ]
then
    echo "配置PHP5.5yum仓库..."
    echo "安装PHP5.5..."
elif [ $num == 3 -o $num == "PHP7.1" ]
then
    echo "配置PHP7.1yum仓库..."
    echo "安装PHP7.1..."
elif [ $num == 4 -o $num == "exit" ]
then
    exit
else
    echo "输入信息有误"
fi

done
[root@LB00 Day03]# sh menu.sh
			 1.PHP5.4 
			 2.PHP5.5 
			 3.PHP7.1 
			 4.exit 
请输入你要安装的版本的编号: 1
配置PHP5.4yum仓库...
安装PHP5.4...
			 1.PHP5.4 
			 2.PHP5.5 
			 3.PHP7.1 
			 4.exit 
请输入你要安装的版本的编号: 2
配置PHP5.5yum仓库...
安装PHP5.5...
			 1.PHP5.4 
			 2.PHP5.5 
			 3.PHP7.1 
			 4.exit 
请输入你要安装的版本的编号: 4
[root@LB00 Day03]#

1. Install different software service versions

Tomcat Apache PHP Nginx

Secondary menu, loop within loop, function call

[root@LB00 Day03]# cat menu2.sh
#!/bin/bash1
while true
do

menu1(){
echo -e "\t\t\t\033[31m 1.PHP \033[0m"
echo -e "\t\t\t\033[32m 2.Tomcat \033[0m"
echo -e "\t\t\t\033[33m 3.Redis \033[0m"
echo -e "\t\t\t\033[34m 4.Mysql \033[0m"
echo -e "\t\t\t\033[35m 5.exit \033[0m"
echo -e "\t\t\t\033[36m h.显示菜单 \033[0m"
}
menu2(){
    echo -e "\t\t\t\033[31m 1.PHP5.4 \033[0m"
    echo -e "\t\t\t\033[31m 2.PHP5.5 \033[0m"
    echo -e "\t\t\t\033[31m 3.PHP7.1 \033[0m"
    echo -e "\t\t\t\033[31m 4.返回主菜单 \033[0m"
    echo -e "\t\t\t\033[31m 5.显示PHP菜单 \033[0m"
}
menu3(){
    echo -e "\t\t\t\033[32m 1.Tomcat7 \033[0m"
    echo -e "\t\t\t\033[32m 2.Tomcat8 \033[0m"
    echo -e "\t\t\t\033[32m 3.Tomcat9 \033[0m"
    echo -e "\t\t\t\033[32m 4.返回主菜单 \033[0m"
    echo -e "\t\t\t\033[32m 5.显示Tomcat菜单 \033[0m"
}
menu1
while true
do
read -p "请输入要安装的服务编号: " num1
if [ $num1 == 1 ]
then 
    menu2
    while true
    do
    read -p "请输入要安装的服务编号: " num_PHP
    if [ $num_PHP -eq 1 ]
    then
        echo "正在安装PHP5.4..."
    elif [ $num_PHP -eq 2 ]
    then
        echo "正在安装PHP5.5..."
    elif [ $num_PHP -eq 3 ]
    then
        echo "正在安装PHP7.1..."
    elif [ $num_PHP -eq 4 ]
    then
        break 2 #跳出2次循环
    elif [ $num_PHP -eq 5 ]
    then
        menu2
    fi
    done
elif [ $num1 == 2 ]
then
    menu3
    while true
    do
    read -p "请输入要安装的具体版本号: " num2
    if [ $num2 -eq 1 ]
    then
        echo "正在安装Tomcat7..."
    elif [ $num2 -eq 2 ]
    then
	echo "正在安装Tomcat8..."
    elif [ $num2 -eq 3 ]
    then
        echo "正在安装Tomcat9..."
    elif [ $num2 -eq 4 ]
    then
        break 2 #跳出两次循环
    elif [ $num2 -eq 5 ]
    then
	menu3
    fi
    done
elif [ $num1 == 3 ]
then
   echo 3
elif [ $num1 == 4 ]
then
   echo 4
elif [ $num1 == 5 ]
then
    exit
elif [ $num1 == h ]
then
    menu1
fi
done

done
[root@LB00 Day03]# sh menu2.sh
			 1.PHP 
			 2.Tomcat 
			 3.Redis 
			 4.Mysql 
			 5.exit 
			 h.显示菜单 
请输入要安装的服务编号: 1
			 1.PHP5.4 
			 2.PHP5.5 
			 3.PHP7.1 
			 4.返回主菜单 
			 5.显示PHP菜单 
请输入要安装的服务编号: 1
正在安装PHP5.4...
请输入要安装的服务编号: 1
正在安装PHP5.4...
请输入要安装的服务编号: 1
正在安装PHP5.4...
请输入要安装的服务编号: 1
正在安装PHP5.4...
请输入要安装的服务编号: 1
正在安装PHP5.4...
请输入要安装的服务编号: 5
			 1.PHP5.4 
			 2.PHP5.5 
			 3.PHP7.1 
			 4.返回主菜单 
			 5.显示PHP菜单 
请输入要安装的服务编号: 4
			 1.PHP 
			 2.Tomcat 
			 3.Redis 
			 4.Mysql 
			 5.exit 
			 h.显示菜单 
请输入要安装的服务编号: 2
			 1.Tomcat7 
			 2.Tomcat8 
			 3.Tomcat9 
			 4.返回主菜单 
			 5.显示Tomcat菜单 
请输入要安装的具体版本号: 2
正在安装Tomcat8...
请输入要安装的具体版本号: 2
正在安装Tomcat8...
请输入要安装的具体版本号: 2
正在安装Tomcat8...
请输入要安装的具体版本号: 5
			 1.Tomcat7 
			 2.Tomcat8 
			 3.Tomcat9 
			 4.返回主菜单 
			 5.显示Tomcat菜单 
请输入要安装的具体版本号: 4
			 1.PHP 
			 2.Tomcat 
			 3.Redis 
			 4.Mysql 
			 5.exit 
			 h.显示菜单 
请输入要安装的服务编号: 5

Note: It is best to use == if to do matching, so that both strings and integers can match; break out of the loop, pay attention to how many times you need to jump, and write a few times after jumping a few times

case process judgment

case 变量 in
    匹配序列1)
    执行命令
    ;;
    匹配序列2)
    执行命令
    ;;
    匹配序列3)
    执行命令
    ;;
    *)
    无匹配后续命令
esac

[root@LB00 Day03]# cat case.sh
#!/bin/bash
case $1 in
	mysql)
	echo mysql......
	;;
	shell)
	echo shell......
	;;
	docker)
	echo docker......
	;;
	*)
	echo "Usage: $0[mysql|shell|docker]"
esac
[root@LB00 Day03]# sh case.sh
Usage: case.sh[mysql|shell|docker]
[root@LB00 Day03]# sh case.sh mysql
mysql......
[root@LB00 Day03]# sh case.sh shell
shell......
[root@LB00 Day03]# sh case.sh docker
docker......

1. Use the menu to view memory, disk, load, and public IP

[root@LB00 Day03]# cat xitong.sh
#!/bin/bash
menu1(){
echo -e "\t\t\t\033[31m 1|f.显示内存 \033[0m"
echo -e "\t\t\t\033[32m 2|d.显示磁盘 \033[0m"
echo -e "\t\t\t\033[33m 3|u.显示负载 \033[0m"
echo -e "\t\t\t\033[34m 4|c.显示公网IP \033[0m"
echo -e "\t\t\t\033[35m 5|h.显示帮助 \033[0m"
echo -e "\t\t\t\033[36m 6|e.exit \033[0m"
}
menu1
while true
do
read -p "请输入你要查看系统信息的编号" num
case $num in
	1|f)
	free -h
	;;
	2|d)
	df -h
	;;
	3|u)
	uptime
	;;
	4|c)
	curl -s cip.cc |awk 'NR==1{print $3}'
	;;
	5|h)
	clear
	menu1
	;;
        6|e)
        exit
        ;;
	*)
	echo "Usage: $0 [1|2|3|4|5|6] [f|d|u|c|h|e]"
esac
done
[root@LB00 Day03]# sh xitong.sh
			 1|f.显示内存 
			 2|d.显示磁盘 
			 3|u.显示负载 
			 4|c.显示公网IP 
			 5|h.显示帮助 
			 6|e.exit 
请输入你要查看系统信息的编号1
              total        used        free      shared  buff/cache   available
Mem:           972M        148M        392M         13M        431M        670M
Swap:          1.0G          0B        1.0G
请输入你要查看系统信息的编号2
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        476M     0  476M   0% /dev
tmpfs           487M     0  487M   0% /dev/shm
tmpfs           487M   14M  473M   3% /run
tmpfs           487M     0  487M   0% /sys/fs/cgroup
/dev/sda3        19G  2.0G   17G  11% /
/dev/sda1       197M  110M   88M  56% /boot
tmpfs            98M     0   98M   0% /run/user/0
请输入你要查看系统信息的编号3
 15:22:53 up 1 day, 15:59,  3 users,  load average: 0.00, 0.01, 0.05
请输入你要查看系统信息的编号4
123.112.17.24
请输入你要查看系统信息的编号5
			 1|f.显示内存 
			 2|d.显示磁盘 
			 3|u.显示负载 
			 4|c.显示公网IP 
			 5|h.显示帮助 
			 6|e.exit 
请输入你要查看系统信息的编号h
			 1|f.显示内存 
			 2|d.显示磁盘 
			 3|u.显示负载 
			 4|c.显示公网IP 
			 5|h.显示帮助 
			 6|e.exit 
请输入你要查看系统信息的编号e

Two, jumperserver springboard machine

Do key-free first

[root@LB00 Day03]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:8oyhGevLUlZPstUAA43+bI1C/xvCEsbLEQEt2RdRheY root@LB00
The key's randomart image is:
+---[RSA 2048]----+
|  .=o+==.o.      |
|  o +.o.+        |
|   o.. o o       |
|   .o.o E .      |
|   .*=oOS        |
|   o+XB*o        |
|   oBo+.+        |
|  .o . ...       |
|   .+.  ..       |
+----[SHA256]-----+
[root@LB00 Day03]# ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
[root@LB00 Day03]# ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
[root@LB00 Day03]# ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
[root@LB00 Day03]# ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
[root@LB00 Day03]# ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]

basic function

[root@LB00 Day03]# cat jumperserver.sh
#!/bin/bash
Web01=10.0.0.7
Web02=10.0.0.8
NFS=10.0.0.31
MySQL=10.0.0.51
Backup=10.0.0.41
menu(){
echo -e "\t\t\t\033[31m 1.Web01|$Web01 \033[0m"
echo -e "\t\t\t\033[32m 2.Web02|$Web02 \033[0m"
echo -e "\t\t\t\033[33m 3.NFS|$NFS \033[0m"
echo -e "\t\t\t\033[34m 4.MySQL|$MySQL \033[0m"
echo -e "\t\t\t\033[35m 5.Backup|$Backup \033[0m"
}
while true
do
menu
read -p "请输入要跳转的服务器编号: " num1
case $num1 in 
	1)
	ssh $Web01
	;;
	2)
        ssh $Web02
        ;;
        3)
        ssh $NFS
        ;;
        4)
        ssh $MySQL
        ;;
        5)
        ssh $Backup
        ;;
        *)
        echo "Usage: [1|2|3|4|5]"
        ;;
esac
done

#加入环境变量实现连接Xshell自动执行脚本,不允许进入命令行
[root@LB00 Day03]# tail -1 /etc/profile
sh /server/scripts/Day03/jumperserver.sh

			 1.Web01|10.0.0.7 
			 2.Web02|10.0.0.8 
			 3.NFS|10.0.0.31 
			 4.MySQL|10.0.0.51 
			 5.Backup|10.0.0.41 
请输入要跳转的服务器编号: 1
Last login: Tue May 16 15:41:54 2023 from 10.0.0.4
[root@Web01 ~]# exit
logout
Connection to 10.0.0.7 closed.
			 1.Web01|10.0.0.7 
			 2.Web02|10.0.0.8 
			 3.NFS|10.0.0.31 
			 4.MySQL|10.0.0.51 
			 5.Backup|10.0.0.41 
请输入要跳转的服务器编号: 

d68d100c5edc40c2b1e7b73516f3c8b6.png

The jumperserver script is simple and perfect

1. Authority control, distinguish operation and maintenance and development menus, main menu: 1. Operation and maintenance; 2. Development

2. Add a password and add the function of changing the password after entering 5 times wrongly

3. Only operation and maintenance can exit (leaving a back door, other roles cannot ctrl+c)

[root@LB00 Day03]# cat jumperserver.sh 
#!/bin/bash
trap "别乱按小心爆炸" INT #禁用ctrl+c
trap "别乱按小心爆炸" HUP #禁用停止进程,进程结束和初始化进程
trap "别乱按小心爆炸" TSTP #禁用ctrl+z
disable_ctrl_z () {
    echo "The Control-Z interrupt is disabled."
    read -p '是你吗' snm
    if [ "$snm" == yes ];then
        trap - INT
        trap - TSTP
        exit
    fi
}
trap disable_ctrl_z SIGTSTP
disable_ctrl_c () {
    echo "The Control-C interrupt is disabled."
}
trap disable_ctrl_c SIGINT

Web01=10.0.0.7
Web02=10.0.0.8
NFS=10.0.0.31
MySQL=10.0.0.51
Backup=10.0.0.41
password_neizhi_kaifa=666
mimashuru=false

menu1(){
echo -e "\t\t\t\033[31m 1.Web01|$Web01 \033[0m"
echo -e "\t\t\t\033[32m 2.Web02|$Web02 \033[0m"
echo -e "\t\t\t\033[33m 3.NFS|$NFS \033[0m"
echo -e "\t\t\t\033[34m 4.MySQL|$MySQL \033[0m"
echo -e "\t\t\t\033[35m 5.Backup|$Backup \033[0m"
}
menu2(){
echo -e "\t\t\t\033[31m 1.运维 \033[0m"
echo -e "\t\t\t\033[32m 2.开发 \033[0m"
echo -e "\t\t\t\033[33m 3.新员工注册身份 \033[0m"
}
while true
do
	menu2
	read -p "请输入您角色编号: " num2
	if [ "$num2" == 1 ];then
		while true
		do
		read -p "欢迎尊敬的运维工程师,请输入您的密码" password_yunwei  #-s隐藏输入内容
        echo ''    #-s会导致下一行格式出问题,所以在read使用-s后,下面紧跟空行解决
		if [ "$password_yunwei" != 123 ];then
		echo "密码输入错误,请重新输入"
		else
		mimashuru=true
		menu1
		break;
		fi
		done
	elif [ "$num2" == 2 ];then
	        while true
                do
		let i++
                read -s -p "欢迎尊敬的开发人员,请输入您的密码" password_kaifa  #-s隐藏输入内容
                echo ''   #-s会导致下一行格式出问题,所以在read使用-s后,下面紧跟空行解决
               		if [ "$password_kaifa" != "$password_neizhi_kaifa" ];then
                		echo "密码输入错误,请重新输入"
                	else
				mimashuru=true
                		menu1
                		break;
                	fi
		if [ "$i" -gt 4 ];then
			echo "输入密码错误次数过多,系统默认为您找回密码"
			read -p "请输入您注册时候的预留邮箱" youxiang
			case $youxiang in
				[email protected])
				echo 邮箱为:[email protected]
				;;
                                [email protected])
                                echo 邮箱为:[email protected]
                                ;;
                                [email protected])
                                echo 邮箱为:[email protected]
                                ;;
                                [email protected])
                                echo 邮箱为:[email protected]
                                ;;
                                *)
                                echo 这个邮箱里系统没有,你到底是不是我们公司的!
				break
                                ;;
			esac
			yzm="$((RANDOM%999999))"
			printf -v yzm "%06d" "$yzm" #不足6位数字,用6为填充
			echo 您正在进行xx公司跳板机密码找回业务,验证码为$yzm,如非本人操作请无视此邮箱,请不要把验证码告诉别人! > /tmp/yzm.log 
			mail -s '找回密码' $youxiang < /tmp/yzm.log &> /dev/null
			echo "已将6位随机验证码发送至您注册时的预留邮箱"	
			sleep 2
			read -p "请输入6位随机验证码" syzm
			if [ "$syzm" == $yzm ];then
			echo "密码输入正确,你密码给你重置成888了"
			password_neizhi_kaifa=888
			menu2
			else
			echo "这你都能输入错啊?"
			break
			fi	
		fi
                done
	elif [ "$num2" == 3 ];then
		echo '功能待开发'
	else 
		echo 'Usage: [1|2|3]'
	fi

	if [ $mimashuru == true ];then
	read -p "请输入要跳转的服务器编号: " num1
	case $num1 in 
		1)
		ssh $Web01
		;;
		2)
	        ssh $Web02
	        ;;
	        3)
	        ssh $NFS
	        ;;
	        4)
	        ssh $MySQL
	        ;;
	        5)
	        ssh $Backup
	        ;;
	        *)
	        echo "Usage: [1|2|3|4|5]"
	        ;;
	esac
	fi
done

[root@LB00 Day03]# sh jumperserver.sh    #测试密码功能,验证码发送邮箱功能
			 1.运维 
			 2.开发 
			 3.新员工注册身份 
请输入您角色编号: 2
欢迎尊敬的开发人员,请输入您的密码1
密码输入错误,请重新输入
欢迎尊敬的开发人员,请输入您的密码1
密码输入错误,请重新输入
欢迎尊敬的开发人员,请输入您的密码1
密码输入错误,请重新输入
欢迎尊敬的开发人员,请输入您的密码1
密码输入错误,请重新输入
欢迎尊敬的开发人员,请输入您的密码1
密码输入错误,请重新输入
输入密码错误次数过多,系统默认为您找回密码
  输入您注册时候的预留邮箱[email protected]
邮箱为:[email protected]
已将6位随机验证码发送至您注册时的预留邮箱
请输入6位随机验证码014565
密码输入正确,你密码给你重置成888了
			 1.运维 
			 2.开发 
			 3.新员工注册身份 
欢迎尊敬的开发人员,请输入您的密码    #隐藏
			 1.Web01|10.0.0.7 
			 2.Web02|10.0.0.8 
			 3.NFS|10.0.0.31 
			 4.MySQL|10.0.0.51 
			 5.Backup|10.0.0.41 
请输入要跳转的服务器编号: ^CThe Control-C interrupt is disabled.    #测试禁用ctrl+c,ctrl+z ,ctrl+z留了后门
^ZThe Control-Z interrupt is disabled.
是你吗yes

4cc7359a30c84d7b91eab203debc2bf1.png

3. Use case to implement Nginx startup script

basic function

/usr/sbin/nginx                                             启动
/usr/sbin/nginx -s stop                                     停止
/usr/sbin/nginx -s reload                                   重新加载
/usr/sbin/nginx -s restart                                  不支持
/usr/sbin/nginx -s stop && sleep 1 && /usr/sbin/nginx       重启
/usr/sbin/nginx -s status                                   不支持
Nginx_port=`netstat -tnulp|grep nginx |awk '{print $4}'`
echo "Nginx_port=$Nginx_port"                               查看状态

[root@LB00 Day03]# cat nginx.sh
#!/bin/bash
case $1 in 
	start)
	/usr/sbin/nginx
	;;
	stop)
	/usr/sbin/nginx -s stop
	;;
	reload)
	/usr/sbin/nginx -s reload
	;;
	restart)
	/usr/sbin/nginx -s stop && sleep 1 && /usr/sbin/nginx
	;;
	status)
	Nginx_port=`netstat -tnulp|grep nginx |awk '{print $4}'`
	echo "Nginx_port=$Nginx_port"
	;;
	*)
	echo "Usage: [start|stop|reload|restart|status]"
esac

perfect version

[root@LB00 Day03]# cat nginx.sh
#!/bin/bash
[ -f /etc/init.d/functions ] && . /etc/init.d/functions
Nginx='/usr/sbin/nginx'
test(){
        if [ $? -eq 0 ];then
        #echo "Nginx启动成功"
        action "Nginx $1 is " /bin/true
        else
        #echo "Nginx停止失败"
        action "Nginx $1 is " /bin/false
        fi
}
case $1 in 
	start)
	$Nginx
	test $1
	;;
	stop)
	$Nginx -s stop
	test $1
	;;
	reload)
	$Nginx -s reload
	test $1
	;;
	restart)
	$Nginx -s stop && sleep 1 && $Nginx
	test $1
	;;
	status)
	Nginx_port=`netstat -tnulp|grep nginx |awk '{print $4}'`
	echo "Nginx_port=$Nginx_port"
	;;
	*)
	echo "Usage: [start|stop|reload|restart|status]"
esac

Combat practice

Carry out some practical exercises, so that learning can achieve twice the result with half the effort.

1. Create and delete users in batches

1. Create users in batches

2. Delete users in batches

read reads the prefix and number of variable users, uses the for loop to display the users that need to be created, prompts whether to create or delete the above users, y to create, d to delete, i to view the user id, the result needs to be prompted

[root@LB00 Day03]# cat yonghu.sh 
#!/bin/bash
read -p "请输入用户的前缀: " qianzhui
if ! [[ $qianzhui =~ ^[a-z] ]];then
	echo 请注意前缀输入格式 
	exit	
fi
read -p "请输入要操作的数量: " num1
if ! [[ $num1 =~ ^[0-9]+$ ]];then
        echo 请注意输入数量的格式
        exit
fi
echo 为您生成如下用户
for i1 in $(seq 1 $num1)
do
	echo $qianzhui$i1
done
read -p "您可以选择操作y为创建,d为删除,i为查看用户id[y|d|i]" caozuo
if [ $caozuo == y ];then
	for i2 in $(seq 1 $num1)
	do
        	useradd $qianzhui$i2
	done		
	echo "用户创建完毕"
elif [ $caozuo == d ];then
	for i3 in $(seq 1 $num1)
        do
                userdel -rf $qianzhui$i3
        done
	echo "用户删除完毕"
elif [ $caozuo == i ];then
        for i4 in $(seq 1 $num1)
        do
                id $qianzhui$i4
        done
        echo "用户查询完毕"
else 
	echo 请输入正确的内容[y|d|i]
fi
[root@LB00 Day03]# sh yonghu.sh 
请输入用户的前缀: qwer
请输入要操作的数量: 5
为您生成如下用户
qwer1
qwer2
qwer3
qwer4
qwer5
您可以选择操作y为创建,d为删除,i为查看用户id[y|d|i]y
用户创建完毕
[root@LB00 Day03]# sh yonghu.sh 
请输入用户的前缀: qwer
请输入要操作的数量: 5
为您生成如下用户
qwer1
qwer2
qwer3
qwer4
qwer5
您可以选择操作y为创建,d为删除,i为查看用户id[y|d|i]i
uid=1002(qwer1) gid=1002(qwer1) groups=1002(qwer1)
uid=1003(qwer2) gid=1003(qwer2) groups=1003(qwer2)
uid=1004(qwer3) gid=1004(qwer3) groups=1004(qwer3)
uid=1005(qwer4) gid=1005(qwer4) groups=1005(qwer4)
uid=1006(qwer5) gid=1006(qwer5) groups=1006(qwer5)
用户查询完毕
[root@LB00 Day03]# sh yonghu.sh 
请输入用户的前缀: qwer
请输入要操作的数量: 5
为您生成如下用户
qwer1
qwer2
qwer3
qwer4
qwer5
您可以选择操作y为创建,d为删除,i为查看用户id[y|d|i]d
用户删除完毕
[root@LB00 Day03]# sh yonghu.sh 
请输入用户的前缀: qwer
请输入要操作的数量: 5
为您生成如下用户
qwer1
qwer2
qwer3
qwer4
qwer5
您可以选择操作y为创建,d为删除,i为查看用户id[y|d|i]i
id: qwer1: no such user
id: qwer2: no such user
id: qwer3: no such user
id: qwer4: no such user
id: qwer5: no such user
用户查询完毕

2. Guess the number game

1. The system randomly generates numbers between 1-100

echo $RANDOM 默认1-32767
echo $((RANDOM%100))    0-99
echo $((RANDOM%100+1))    1-100

2. read allows the user to enter a number between 1-100

If it is less than random, prompt the user that it is too large

If it is less than random, prompt the user that it is too small

3. Keep guessing if you don’t guess right, if you guess right, you will be prompted to succeed, and exit the script

[root@LB00 Day03]cat caishu.sh
#!/bin/bash
sy_ran=`echo $((RANDOM%100+1))`
while true
do
read -p "请输入你要猜的数字[1-100]" num
if [ $num -gt $sy_ran ];then
	echo "你输入的大了"
elif [ $num -lt $sy_ran ];then
	echo "你输入的小了"
else 
	echo "恭喜你猜对了,奖励你猜对号码对应的RMB"
	echo "数额为: $sy_ran"
        break
fi
done

You can further improve the function of the amount and the number of times

[root@LB00 Day03]# cat caishu.sh
#!/bin/bash
sy_ran=`echo $((RANDOM%100+1))`

chongzhi(){
read -p "请输入充值的金额[1|2|3..10RRMB]: " jine
echo "恭喜你获得"$jine"次抽奖机会"
if ! [[ $jine =~ ^[0-9]+$ ]];then
        echo 请输入数字
        exit
fi
}
chongzhi
while true
do
if [ $jine -gt 0 ];then
jine=`echo "$jine-1"|bc`

let i++
read -p "请输入你要猜的数字[1-100]" num
if [[ ! $num =~ ^[0-9]+$ ]];then
	echo 请输入数字
	exit
fi
if [ $num -gt $sy_ran ];then
	echo "你输入的大了"
elif [ $num -lt $sy_ran ];then
	echo "你输入的小了"
else 
	echo "恭喜你猜对了,奖励你猜对号码对应的RMB"
	echo "数额为: $sy_ran"
	echo "共猜了"$i"次"
        break
fi
else
  echo "没有摇奖机会了,你可以选择继续充值"
  chongzhi
fi
done
[root@LB00 Day03]# sh caishu.sh
请输入充值的金额[1|2|3..10RRMB]: 5
恭喜你获得5次抽奖机会
请输入你要猜的数字[1-100]45
你输入的小了
请输入你要猜的数字[1-100]87
你输入的大了
请输入你要猜的数字[1-100]67
你输入的大了
请输入你要猜的数字[1-100]57
你输入的小了
请输入你要猜的数字[1-100]60
你输入的大了
没有摇奖机会了,你可以选择继续充值
请输入充值的金额[1|2|3..10RRMB]: 5
恭喜你获得5次抽奖机会
请输入你要猜的数字[1-100]58
恭喜你猜对了,奖励你猜对号码对应的RMB
数额为: 58
共猜了6次

It can be expanded and let the system guess by itself

[root@LB00 Day03]# cat caishu.sh
#!/bin/bash
menu(){
echo -e "\t\t\t\033[31m 1.系统帮你猜 \033[0m"
echo -e "\t\t\t\033[32m 2.自己手动猜 \033[0m"
}
sy_ran=`echo $((RANDOM%100+1))`
da=100
xiao=1
num=0
chongzhi(){
read -p "请输入充值的金额[1|2|3..10RRMB]: " jine
echo "恭喜你获得"$jine"次抽奖机会"
if ! [[ $jine =~ ^[0-9]+$ ]];then
        echo 请输入数字
        exit
fi
}
chongzhi
menu
read -p "请您输入编号选择猜数方式[1|2]: " fangshi
if [ $fangshi == 1 ];then
	echo 您选择的是系统帮你猜,祝您好运!
	while true
        do
        if [ $jine -gt 0 ];then
        jine=`echo "$jine-1"|bc`

        let i++
		if [ $num == 0 ];then
		num=`echo $((RANDOM%100+1))`
		else
			if [ $shangci == da ];then
			da=`echo "$num-1"|bc`
			#echo $da $xiao
			num=`shuf -i $xiao-$da -n 1`
			else
			xiao=`echo "$num+1"|bc`
			#echo $da $xiao
			num=`shuf -i $xiao-$da -n 1`
		        fi
		fi
        	echo "系统帮你猜的数字是$num"
		sleep 1
        	if [[ ! $num =~ ^[0-9]+$ ]];then
        	        echo 请输入数字
        	        exit
        	fi
        	if [ $num -gt $sy_ran ];then
        	        echo "系统猜的数字大了"
			shangci=da
        	elif [ $num -lt $sy_ran ];then
        	        echo "系统猜的数字小了"
			shangci=xiao
        	else
        	        echo "恭喜你,系统帮你猜对了,奖励是猜对号码对应的RMB"
        	        echo "数额为: $sy_ran"
        	        echo "共猜了"$i"次"
        	        break
        	fi
        else
          echo "没有摇奖机会了,你可以选择继续充值"
          chongzhi
        fi
        done
else
	echo 您选择的是自己手动猜,祝您好运!
	while true
	do
	if [ $jine -gt 0 ];then
	jine=`echo "$jine-1"|bc`
	
	let i++
	read -p "请输入你要猜的数字[1-100]" num
	if [[ ! $num =~ ^[0-9]+$ ]];then
		echo 请输入数字
		exit
	fi
	if [ $num -gt $sy_ran ];then
		echo "你输入的大了"
	elif [ $num -lt $sy_ran ];then
		echo "你输入的小了"
	else 
		echo "恭喜你猜对了,奖励你猜对号码对应的RMB"
		echo "数额为: $sy_ran"
		echo "共猜了"$i"次"
	        break
	fi
	else
	  echo "没有摇奖机会了,你可以选择继续充值"
	  chongzhi
	fi
	done
fi
[root@LB00 Day03]# sh caishu.sh
请输入充值的金额[1|2|3..10RRMB]: 10
恭喜你获得10次抽奖机会
			 1.系统帮你猜 
			 2.自己手动猜 
请您输入编号选择猜数方式[1|2]: 1
您选择的是系统帮你猜,祝您好运!
系统帮你猜的数字是21
系统猜的数字小了
系统帮你猜的数字是95
系统猜的数字大了
系统帮你猜的数字是38
系统猜的数字大了
系统帮你猜的数字是22
系统猜的数字小了
系统帮你猜的数字是27
系统猜的数字小了
系统帮你猜的数字是37
系统猜的数字大了
系统帮你猜的数字是30
系统猜的数字大了
系统帮你猜的数字是29
恭喜你,系统帮你猜对了,奖励是猜对号码对应的RMB
数额为: 29
共猜了8次

I am koten, with 10 years of experience in operation and maintenance, and I continue to share dry goods in operation and maintenance. Thank you for your reading and attention!

 

Guess you like

Origin blog.csdn.net/qq_37510195/article/details/130697597