菜单
本页目录

四 条件判断 [ ]

1 按照文件类型进行判断

选项作用
-b 文件文件存在、且为块设备文件、则为真
-c 文件文件存在、且为字符设备文件、则为真
-d 文件文件存在、且为目录、则为真
-e 文件文件存在则为真(此判断更宽泛)
-f 文件文件存在、且为普通文件、则为真
-L 文件文件存在、且为软链接、则为真
-p 文件文件存在、且为管道文件、则为真
-s 文件文件存在、且内容非空、则为真
-S 文件文件存在、且为套接字文件、则为真

命令1 && 命令2 #命令1成功则执行命令2

命令1 || 命令2 #命令1成功,则不执行命令2;命令1不执行成功,则执行命令2

[root@iKuaiOS ~]# [ -b "/dev/sr0" ] && echo "yes" ||echo "no"			#文件存在、且为块设备文件、则为真
yes
[root@iKuaiOS ~]# [ -b "/etc/fstab" ] && echo "yes" ||echo "no"
no
[root@iKuaiOS ~]# [ -c "/dev/tty" ] && echo "yes" || echo "no"			#文件存在、且为字符设备文件、则为真
yes
[root@iKuaiOS ~]# [ -d "/dev/" ] && echo "yes" ||echo "no"				#文件存在、且为目录、则为真
yes
[root@iKuaiOS ~]# [ -d "/dev/sr0" ] && echo "yes" ||echo "no"
no
[root@iKuaiOS ~]# [ -e "/bin" ] && echo "yes" || echo "no"				#文件存在则为真(此判断更宽泛)
yes			
[root@iKuaiOS ~]# [ -f "/dev/sr0" ] && echo "yes" ||echo "no"			#文件存在、且为普通文件、则为真
no
[root@iKuaiOS ~]# [ -f "/etc/fstab" ] && echo "yes" ||echo "no"
yes
[root@iKuaiOS ~]# [ -L "/bin" ] && echo "yes" || echo "no"				#文件存在、且为软链接、则为真
yes
[root@iKuaiOS ~]# find / -type p
/run/systemd/inhibit/1.ref
/run/systemd/sessions/4.ref
/run/systemd/sessions/2.ref
/run/systemd/sessions/1.ref
/run/systemd/initctl/fifo
[root@iKuaiOS ~]# [ -p "/run/systemd/inhibit/1.ref" ] && echo "yes" || echo "no"		#文件存在、且为管道文件、则为真
yes
[root@iKuaiOS ~]# [ -s "/etc/fstab" ] && echo "yes" || echo "no"					#文件存在、且内容非空、则为真
yes
[root@iKuaiOS ~]# find / -type s |grep dev
/dev/log
/run/udev/control
[root@iKuaiOS ~]# [ -S "/dev/log" ] && echo "yes" || echo "no"					#文件存在、且为套接字文件、则为真
yes

2 按照文件权限进行判断

选项作用
-r 文件文件有读权限则为真
-w 文件文件有写权限则为真
-x 文件文件有执行权限则为真
-u 文件文件有SUID权限则为真 /usr/bin/passwd
-g 文件文件有SGID权限则为真 /usr/bin/locate
-k 文件文件有SBit权限则为真 /tmp
[yq1@iKuaiOS ~]$ ls -l /etc/passwd /etc/shadow
-rw-r--r--. 1 root root 941 10月  7 23:27 /etc/passwd
----------. 1 root root 733 10月  8 14:53 /etc/shadow
[yq1@iKuaiOS ~]$ 
[yq1@iKuaiOS ~]$ [ -r "/etc/passwd" ] && echo "yes" || echo "no"				#文件有读权限则为真
yes
[yq1@iKuaiOS ~]$ [ -r "/etc/shadow" ] && echo "yes" || echo "no"	
no
[yq1@iKuaiOS ~]$ [ -w "/etc/passwd" ] && echo "yes" || echo "no"				#文件有写权限则为真
no
[yq1@iKuaiOS ~]$ [ -w "/etc/shadow" ] && echo "yes" || echo "no"
no
[yq1@iKuaiOS ~]$ [ -x "/etc/passwd" ] && echo "yes" || echo "no"				#文件有执行权限则为真
no
[yq1@iKuaiOS ~]$ [ -x "/etc/shadow" ] && echo "yes" || echo "no"
no
[yq1@iKuaiOS ~]$ [ -u "/usr/bin/passwd" ] && echo "yes" || echo "no"			#文件有SUID权限则为真 
yes
[yq1@iKuaiOS ~]$ [ -g "/usr/bin/locate" ] && echo "yes" || echo "no"			#文件有SGID权限则为真
yes
[yq1@iKuaiOS ~]$ ls -ld /tmp
drwxrwxrwt. 8 root root 172 10月  8 14:56 /tmp
[yq1@iKuaiOS ~]$ [ -k "/tmp" ] && echo "yes" || echo "no"						#文件有SBit权限则为真
yes

3 两个文件之间进行比较

选项作用
文件1 -nt 文件2文件1比文件2新则为真
文件1 -ot 文件2文件1比文件2旧则为真
文件1 -ef 文件2文件1和文件2inode号一样则为真
[root@iKuaiOS ~]# ls -l /etc/passwd /etc/shadow
-rw-r--r--. 1 root root 941 10月  7 23:27 /etc/passwd
----------. 1 root root 733 10月  8 14:53 /etc/shadow
[root@iKuaiOS ~]# [ "/etc/shadow" -nt "/etc/passwd" ] && echo "yes" || echo "no"				#文件1比文件2新则为真
yes
[root@iKuaiOS ~]# [ "/etc/shadow" -ot "/etc/passwd" ] && echo "yes" || echo "no"				#文件1比文件2旧则为真
no
[root@iKuaiOS ~]# [ "/etc/shadow" -ef "/etc/passwd" ] && echo "yes" || echo "no"				#文件1和文件2inode号一样则为真
no
[root@iKuaiOS ~]# ln /etc/fstab /root/
[root@iKuaiOS ~]# [ "/etc/fstab" -ef "/root/fstab" ] && echo "yes" || echo "no"
yes
[root@iKuaiOS ~]# ls -li /etc/fstab  /root/fstab 
16777616 -rw-r--r--. 2 root root 556 10月  7 23:17 /etc/fstab
16777616 -rw-r--r--. 2 root root 556 10月  7 23:17 /root/fstab

4 整数间比较

/etc/profile(环境变量配置文件)中利用整数判断uid进行进一步操作

选项作用
-eq相等则为真
-ne不相等则为真
-gt大于则为真
-lt小于则为真
-ge大于等于则为真
-le小于等于则为真
[root@iKuaiOS ~]# [ "5" -eq "5" ] && echo "yes" || echo "no"
yes
[root@iKuaiOS ~]# [ "5" -ne "4" ] && echo "yes" || echo "no"
yes
[root@iKuaiOS ~]# [ "5" -gt "4" ] && echo "yes" || echo "no"
yes
[root@iKuaiOS ~]# [ "5" -lt "4" ] && echo "yes" || echo "no"
no
[root@iKuaiOS ~]# [ "5" -ge "4" ] && echo "yes" || echo "no"
yes
[root@iKuaiOS ~]# [ "4" -le "5" ] && echo "yes" || echo "no"
yes

5 字符串判断

选项作用
-z 变量变量为空则为真
-n 变量变量非空则为真
==变量相等则为真
!=变量不等则为真
[root@iKuaiOS ~]# aa1=
[root@iKuaiOS ~]# aa2=123
[root@iKuaiOS ~]# [ -z "$aa1" ] && echo "yes" || echo "no"			#变量为空则为真
yes
[root@iKuaiOS ~]# [ -z "$aa2" ] && echo "yes" || echo "no"
no
[root@iKuaiOS ~]# [ -n "$aa1" ] && echo "yes" || echo "no"			#变量非空则为真
no
[root@iKuaiOS ~]# [ -n "$aa2" ] && echo "yes" || echo "no"
yes
[root@iKuaiOS ~]# aa3=123
[root@iKuaiOS ~]# [ "$aa2" == "$aa3" ] && echo "yes" || echo "no"		#变量相等则为真
yes
[root@iKuaiOS ~]# [ "$aa2" != "$aa3" ] && echo "yes" || echo "no"		#变量不等则为真
no


6 多重雕件判断

选项作用
判断1 -a 判断2两个判断都为真,则为真
判断1 -o 判断2两判断有一个为真,则为真
逻辑非,使原始判断取反 [ ! -n "变量" ]
[root@iKuaiOS ~]# [ -z "$aa1" -a -n "$aa2" ] && echo "yes" || echo "no"			#两个判断都为真,则为真
yes	
[root@iKuaiOS ~]# [ -z "$aa1" -o -n "$aa1" ] && echo "yes" || echo "no"			#两判断有一个为真,则为真
yes
[root@iKuaiOS ~]# [ ! -n "$aa2" ] && echo "yes" || echo "no"			#逻辑非,使原始判断取反
no

五 语句

1 if语句

1)单分支
    if	[条件判断式];then
            程序
    fi

或者

    if	[条件判断式]
    then
    		程序
    fi
2)双分支
if	[条件判断式]
then
	条件成立则执行的程序
else
	条件不成立则执行的程序
fi

写一个脚本,用双分支写一个判断服务状态并重启(nmap)

nmap  -sT  域名或IP
		-s				扫描
		-T				扫描所有开启的TCP端口
[root@iKuaiOS ~]# nmap -sT 192.168.18.134 |grep http |grep 80 |awk '{print $1}' |cut -d "/" -f 1
80
[root@iKuaiOS ~]# cat ./yqif1.sh 
#!/bin/bash
aa1=$(nmap -sT 192.168.18.134 |grep http |grep 80 |awk '{print $1}' |cut -d "/" -f 1)
if [ "$aa1" == "80" ];then
	echo "httpd服务正常运行-----"
	echo "$(date)httpd服务正常运行-----" >> /root/service.log
else
	echo "httpd服务未在运行----正在启动服务----"
	systemctl start httpd  
	echo "服务重启成功------"
	echo "$(date)服务重启----" >> /root/service.err.log
fi
[root@iKuaiOS ~]# systemctl stop httpd
[root@iKuaiOS ~]# ./yqif1.sh 
httpd服务未在运行----正在启动服务----
服务重启成功------
[root@iKuaiOS ~]# ./yqif1.sh 
httpd服务正常运行-----
[root@iKuaiOS ~]# cat ./service.log 
2022年 10月 08日 星期六 15:35:07 CSThttpd服务正常运行-----
2022年 10月 08日 星期六 15:45:36 CSThttpd服务正常运行-----
[root@iKuaiOS ~]# cat ./service.err.log 
2022年 10月 08日 星期六 15:35:45 CST服务重启----
2022年 10月 08日 星期六 15:39:15 CST服务重启----
2022年 10月 08日 星期六 15:40:42 CST服务重启----
2022年 10月 08日 星期六 15:41:15 CST服务重启----
2022年 10月 08日 星期六 15:45:31 CST服务重启----
3) 多分支
if	[条件判断式1]
then
		当条件1成立时则执行的程序
elif	[条件判断式2]
then
		当条件2成立时则执行的程序
。。。。。(可加入更多条件)
else
		当所有条件都不成立时,则执行的程序
fi

写一个脚本,判断所给目录或文件时什么文件类型

[root@iKuaiOS ~]# cat ./yqif2.sh 
#!/bin/bash
read -p "请输入目录或文件:" aa1
if [ -b "$aa1" ];then
	echo "$aa1是块设备文件"
elif [ -c "$aa1" ];then
	echo "$aa1是字符设备文件"
elif [ -L "$aa1" ];then
	echo "$aa1是软链接"
elif [ -p "$aa1" ];then
	echo "$aa1是管道文件"
elif [ -S "$aa1" ];then
	echo "$aa1是套接字文件"
elif [ -d "$aa1" ];then
	echo "$aa1是目录"
elif [ -s "$aa1" ];then
	echo "$aa1是非空的普通文件"
elif [ -f "$aa1" ];then
	echo "$aa1是普通文件"
elif [ -e "$aa1" ];then
	echo "$aa1文件存在"
else
	echo "请输入正确的文件或者目录"
fi
[root@iKuaiOS ~]# ./yqif2.sh 
请输入目录或文件:/dev/sr0
/dev/sr0是块设备文件
[root@iKuaiOS ~]# ./yqif2.sh 
请输入目录或文件:/dev/tty
/dev/tty是字符设备文件
[root@iKuaiOS ~]# find / -type l |head -n 3
/dev/cdrom
/dev/snd/by-path/pci-0000:02:02.0
/dev/initctl
[root@iKuaiOS ~]# ./yqif2.sh 
请输入目录或文件:/dev/cdrom
/dev/cdrom是块设备文件
[root@iKuaiOS ~]# ./yqif2.sh 
请输入目录或文件:/dev/initctl
/dev/initctl是软链接
[root@iKuaiOS ~]# find / -type p |head -n 3
/run/systemd/ask-password-block/136:0
/run/systemd/ask-password-block/136:1
/run/systemd/inhibit/1.ref
[root@iKuaiOS ~]# ./yqif2.sh 
请输入目录或文件:/run/systemd/ask-password-block/136:0
/run/systemd/ask-password-block/136:0是管道文件
[root@iKuaiOS ~]# find / -type s |head -n 3
/dev/log
/run/NetworkManager/private-dhcp
/run/vmware/guestServicePipe
[root@iKuaiOS ~]# ./yqif2.sh 
请输入目录或文件:/dev/log
/dev/log是套接字文件
[root@iKuaiOS ~]# ./yqif2.sh 
请输入目录或文件:/root
/root是目录
[root@iKuaiOS ~]# ./yqif2.sh 
请输入目录或文件:/etc/fstab
/etc/fstab是非空的普通文件
[root@iKuaiOS ~]# touch ./yq.txt 
[root@iKuaiOS ~]# ./yqif2.sh 
请输入目录或文件:/root/yq.txt
/root/yq.txt是普通文件

2 多分支case语句

case  变量  in
		"值1")
				程序1
		    ;;
		"值2")
				程序2
			;;
		.。。。。。。
		*)
				所有值中都没有时,则执行的程序
		;;
esac

注意:值中可以用管道符适用多个选项

写脚本:

1		case语句判断文件名
2		case语句实现服务启动、停止
3		case语句实现网络源、本地源、selinux状态、修改网卡配置	
#case语句判断文件名
[root@iKuaiOS ~]# cat ./case1.sh 
#!/bin/bash
read -p "请输入文件名(/etc/fstab|/etc/passwd):" aa1
case $aa1 in 
	"/etc/fstab")
		echo "您输入的是/etc/fstab"
	;;
	"/etc/passwd")
		echo "您输入的是/etc/passwd"
	;;
	*)
		echo "请输入正确的文件名(/etc/fstab|/etc/passwd)"
	;;
esac
[root@iKuaiOS ~]# ./case1.sh 
请输入文件名(/etc/fstab|/etc/passwd):/etc/fstab
您输入的是/etc/fstab
[root@iKuaiOS ~]# ./case1.sh 
请输入文件名(/etc/fstab|/etc/passwd):/etc/passwd
您输入的是/etc/passwd
[root@iKuaiOS ~]# ./case1.sh 
请输入文件名(/etc/fstab|/etc/passwd):/root
请输入正确的文件名(/etc/fstab|/etc/passwd)

#case语句实现服务启动、停止
[root@iKuaiOS ~]# cat ./case2.sh 
#!/bin/bash
read -p "请输入选择的服务状态(network|ssh|http):" aa1
aa2=$(ifconfig ens33 |grep inet |grep -v inet6 | awk '{print $2}')
aa3=$(nmap -sT ${aa2} |grep ${aa1} |grep open |awk '{print $1}')
aa5=$(systemctl status network |grep Active |awk '{printf $2$3"\n"}')
case $aa1 in
	"ssh")
		if [ "$aa3" == "22/tcp" ];then
			echo "sshd服务正常运行---"
		else
			read -n 1 -p "sshd服务未运行,是否启动(y/n)" aa4
			if [ "$aa4" == "y" ];then
				echo "正在启动sshd----"
				systemctl start sshd
				if [ $? -eq 0 ];then
					echo "服务启动成功---"
				else
					echo "服务启动失败---"
				fi
			else
				echo "正在退出----"
			fi
		fi
	;;
	"http")
		if [ "$aa3" == "80/tcp" ];then
			echo "http服务正常运行---"
		else
			read -n 1 -p "http服务未运行,是否启动(y/n)" aa4
			if [ "$aa4" == "y" ];then
				echo "正在启动httpd----"
				systemctl start httpd
				if [ $? -eq 0 ];then
					echo "服务启动成功---"
				else
					echo "服务启动失败---"
				fi
			else
				echo "正在退出----"
			fi
		fi
	;;
	"network")
		if [ "$aa5" == "active(exited)" ];then
			echo "network正常运行-----"	
		else
			read -n 1 -p "network服务未运行,是否启动(y/n)" aa4
			if [ "$aa4" == "y" ];then
				echo "正在启动network----"
				systemctl start network
				if [ $? -eq 0 ];then
					echo "服务启动成功---"
				else
					echo "服务启动失败---"
				fi
			else
				echo "正在退出----"
			fi
		fi
			
	;;
	*)
		echo "请输入存在的服务(network|ssh|http)"
	;;
esac
[root@iKuaiOS ~]# ./case2.sh 
请输入选择的服务状态(network|ssh|http):network
network正常运行-----
[root@iKuaiOS ~]# ./case2.sh 
请输入选择的服务状态(network|ssh|http):ssh
sshd服务正常运行---
[root@iKuaiOS ~]# ./case2.sh 
请输入选择的服务状态(network|ssh|http):http
http服务正常运行---
[root@iKuaiOS ~]# systemctl stop httpd
[root@iKuaiOS ~]# ./case2.sh 
请输入选择的服务状态(network|ssh|http):http
http服务未运行,是否启动(y/n)y正在启动httpd----
服务启动成功---

#case语句实现网络源、本地源、selinux状态、修改网卡配置
[root@iKuaiOS ~]# cat ./case3.sh 
#!/bin/bash
cat << EOF
1.配置本地源
2.配置网络源
3.配置selinux状态
4.修改网卡模式
EOF
read -n 1 -p "请输入选项:" aaa1
echo ""
case $aaa1 in
	"1")
		read -p "请输入挂载点:" aaa3
		[ -d "$aaa3" ] || mkdir $aaa3
		aaa11=$(df -h |grep /dev/sr0 |awk '{print $1}')
		if [ -n "$aaa11" ];then
			for i in {1..5}
			do
				umount /dev/sr0 
			done
		fi
		mount -t iso9660 /dev/sr0 $aaa3
		if [ $? -eq 0 ];then
			echo "光盘挂载成功---"
		else
			echo "光盘挂载失败---"
		fi
		[ -f "/etc/yum.repos.d/CentOS-Base.repo.bak" ] || mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
		aaa2=$(ls /etc/yum.repos.d/ |grep Media
)
		echo "正在修改本地源-----"
		if [ "$aaa2" == "CentOS-Media.repo" ];then
			aaa4=$(cat -n /etc/yum.repos.d/CentOS-Media.repo |grep "baseurl" |awk '{print $1}')
			aaa5=$(cat -n /etc/yum.repos.d/CentOS-Media.repo |grep "file:///media/cdrom/" |awk '{print $1}')
			aaa6=$(cat -n /etc/yum.repos.d/CentOS-Media.repo |grep "file:///media/cdrecorder/" |awk '{print $1}')
			aaa7=$(cat -n /etc/yum.repos.d/CentOS-Media.repo |grep "enabled" |awk '{print $1}')
			sed -i "${aaa5}s/^/#/;${aaa6}s/^/#/;${aaa7}s/0/1/1;${aaa4}c baseurl=file://$aaa3" /etc/yum.repos.d/CentOS-Media.repo
		else
			cat << EOF > /etc/yum.repos.d/CentOS-Media.repo
[yq]
name=yq
baseurl=file://$aaa3
gpgcheck=0
enabled=1
EOF
		fi
		if [ $? -eq 0 ];then
			echo "本地源修改成功----"
		fi
		yum clean all ; yum makecache
	;;
	"2")
		[ -f "/etc/yum.repos.d/CentOS-Base.repo" ] || mv /etc/yum.repos.d/CentOS-Base.repo.bak /etc/yum.repos.d/CentOS-Base.repo
		echo "网络源文件配置成功-----"
		aaa7=$(cat -n /etc/yum.repos.d/CentOS-Media.repo |grep "enabled" |awk '{print $1}')
		echo "正在关闭本地源----"
		sed -i "${aaa7}c enabled=0" /etc/yum.repos.d/CentOS-Media.repo
		if [ $? -eq 0 ];then
			echo "$(date)本地源关闭成功----" 
			echo "$(date)本地源关闭成功----" >> /root/case.txt
		fi
	;;
	"3")
		read -p "请选择selinux状态(enforcing|disabled):" aaa8
		aaa9=$(cat -n /etc/selinux/config |grep "SELINUX=enforcing" || cat -n /etc/selinux/config |grep "SELINUX=disabled")
		aaa10=$(echo "$aaa9" |awk '{print $1}')
		if [ "$aaa8" == "enforcing" ];then
			sed -i "${aaa10}c SELINUX=$aaa8" /etc/selinux/config
			aaa9=$(cat -n /etc/selinux/config |grep "SELINUX=enforcing" || cat -n /etc/selinux/config |grep "SELINUX=disabled")
			echo "修改成功------"
			echo "${aaa9}"
		else
			if [ "$aaa8" == "disabled" ];then
			sed -i "${aaa10}c SELINUX=$aaa8" /etc/selinux/config
			aaa9=$(cat -n /etc/selinux/config |grep "SELINUX=enforcing" || cat -n /etc/selinux/config |grep "SELINUX=disabled")
			echo "修改成功------"
			echo "${aaa9}"
			else
				echo "请输入正确选项----"
				exit
			fi
		fi	
	;;	
	"4")
	read -p "请选择ip获取方式[d(dhcp)/s(static)]:" aa1
	bb1=$(grep -n "BOOTPROTO" /etc/sysconfig/network-scripts/ifcfg-ens33 | awk -F ":" '{print $1}')
	bb2=$(grep -n "ONBOOT" /etc/sysconfig/network-scripts/ifcfg-ens33 |awk -F ":" '{print $1}')
	if [ -n "$aa1" ];then
		if [ "$aa1" == "s" ];then
			sed -i "${bb1}c BOOTPROTO=static" /etc/sysconfig/network-scripts/ifcfg-ens33
			read -p "请输入ip:" cc1
			echo "$cc1" > /root/ip.txt
			hh1=$(egrep "((^[1-9])|(^[1-9][0-9])|(^1[0-9][0-9])|(^2[0-4][0-9])|(^25[0-5])|[^127])\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$" /root/ip.txt)
			if [ -n "$hh1" ];then
				ff1=$(grep -n "IPADDR" /etc/sysconfig/network-scripts/ifcfg-ens33 |cut -d ":" -f 1)
				if [ -z "$ff1" ];then
					echo "IPADDR=$cc1" >> /etc/sysconfig/network-scripts/ifcfg-ens33
				else
					sed -i "${ff1}c IPADDR=$cc1" /etc/sysconfig/network-scripts/ifcfg-ens33
				fi
			else
				echo "您的输入有误------正在退出------"
				exit
			fi
			rm -rf /root/ip.txt
			read -p "是否使用默认netmask为255.255.255.0(y/n):" cc2
			ff2=$(grep -n "NETMASK" /etc/sysconfig/network-scripts/ifcfg-ens33 |cut -d ":" -f 1) 
			if [ -z "$ff2" ];then
				if	[ "$cc2" == "y" ];then
					echo "NETMASK=255.255.255.0" >> /etc/sysconfig/network-scripts/ifcfg-ens33
				else
					read -p "请输入netmask:" ee1
					echo "$ee1" > /root/ip1.txt
					hh2=$(egrep "^255\.(0|255)\.(0|255)\.(0|255)$" /root/ip1.txt )
					if [ -n "$hh2" ];then
						echo "NETMASK=$ee1" >> /etc/sysconfig/network-scripts/ifcfg-ens33
					else
						echo "您的输入有误------正在退出------"
						exit
					fi
					rm -rf /root/ip1.txt
				fi
			else
				if	[ "$cc2" == "y" ];then
					sed -i "${ff2}c NETMASK=255.255.255.0" /etc/sysconfig/network-scripts/ifcfg-ens33
				else
					read -p "请输入netmask:" ee1
					echo "$ee1" > /root/ip1.txt
					hh2=$(egrep "^255\.(0|255)\.(0|255)\.(0|255)$" /root/ip1.txt )
					if [ -n "$hh2" ];then
						sed -i "${ff2}c NETMASK=$hh2" /etc/sysconfig/network-scripts/ifcfg-ens33
					else	
						echo "您的输入有误------正在退出------"
						exit
					fi
					rm -rf /root/ip1.txt
				fi
			fi
			read -p "是否使用默认网关为192.168.18.2(y/n):" cc3
			ff3=$(grep -n "GATEWAY" /etc/sysconfig/network-scripts/ifcfg-ens33 |cut -d ":" -f 1)  
			if [ -z "$ff3" ];then
				if [ "$cc3" == "y" ];then
					echo "GATEWAY=192.168.18.2" >> /etc/sysconfig/network-scripts/ifcfg-ens33
				else
					read -p "请输入网关(GATEWAY):" cc3
					echo "GATEWAY=$cc3" >> /etc/sysconfig/network-scripts/ifcfg-ens33
				fi
			else
				if [ "$cc3" == "y" ];then
					sed -i "${ff3}c GATEWAY=192.168.18.2" /etc/sysconfig/network-scripts/ifcfg-ens33
				else
					read -p "请输入网关(GATEWAY):" cc3
					sed -i "${ff3}c GATEWAY=$cc3" /etc/sysconfig/network-scripts/ifcfg-ens33
				fi
			fi
			read -p "是否使用默认DNS114.114.114.114(y/n):" cc4
			ff4=$(grep -n "DNS1" /etc/sysconfig/network-scripts/ifcfg-ens33 |cut -d ":" -f 1)
			if [ -z "$ff4" ];then
				if [ "$cc4" == "y" ];then
					echo "DNS1=114.114.114.114" >> /etc/sysconfig/network-scripts/ifcfg-ens33
				else
					read -p "请输入DNS:" cc4
					echo "DNS1=$cc4" >> /etc/sysconfig/network-scripts/ifcfg-ens33
				fi
			else
				if [ "$cc4" == "y" ];then
					sed -i "${ff4}c DNS1=114.114.114.114" /etc/sysconfig/network-scripts/ifcfg-ens33
				else
					read -p "请输入DNS:" cc4
					sed -i "${ff4}c DNS1=$cc4" /etc/sysconfig/network-scripts/ifcfg-ens33
				fi
			fi
		else
			if [ "$aa1" != "d" ];then
				echo "您的输入有误------正在退出------"
				exit	
			else
				sed  -i "${bb1}c BOOTPROTO=dhcp" /etc/sysconfig/network-scripts/ifcfg-ens33
			fi
		fi
	else
		echo "您未选择------正在退出------"
		exit
	fi

	read -p "请选择是否开机自动配ip(y/n):" bb3
	if [ -n "$bb3" ];then
		if [ "$bb3" == "y" ];then
			sed -i "${bb2}c ONBOOT=yes" /etc/sysconfig/network-scripts/ifcfg-ens33
		else
			read -p "请注意----当前终端将会断开,且服务器将没有ip-------是否继续(y/n):" ee2
			if [ "$ee2" == "y" ];then
				sed -i "${bb2}c ONBOOT=no" /etc/sysconfig/network-scripts/ifcfg-ens33
			else
				exit
			fi
		fi
	else
	echo "您未选择-----正在进行下一个选择--------"
	fi

	echo "请注意------重启网卡后,当前终端可能会断开-------"
	read -p "是否重启网卡(y/n):" bb4
	if [ -n "$bb3" ];then
		if [ "$bb4" == "y" ];then
			if [ "$aa1" == "s" ];then
				echo "您的IP是:$cc1"
			else
				dd1=$(ip addr | grep "inet 192" |awk -F " " '{print $2}' |cut -d "/" -f 1)
				echo "您的IP是:$dd1"
			fi
			echo "正在重启网卡-----"  
			echo "如果远程连接断开,请根据上面IP重新远程连接-----------------"
			systemctl restart network 
		else
			echo "若不重启网卡,需要您手动重启网卡-------"
		fi
	else
		echo "您未选择-------正在退出--------"
	fi
	;;
	*)
		echo "请输入正确的选项---"
	;;
esac	
[root@iKuaiOS ~]# ./case3.sh 				#进行测试
1.配置本地源
2.配置网络源
3.配置selinux状态
4.修改网卡模式
请输入选项:1
请输入挂载./disk1           
umount: /dev/sr0:未挂载
umount: /dev/sr0:未挂载
umount: /dev/sr0:未挂载
umount: /dev/sr0:未挂载
mount: /dev/sr0 写保护,将以只读方式挂载
光盘挂载成功---
正在修改本地源-----
本地源修改成功----
已加载插件:fastestmirror
正在清理软件源: c7-media
Cleaning up list of fastest mirrors
已加载插件:fastestmirror
Determining fastest mirrors
c7-media                                                                                                                  | 3.6 kB  00:00:00     
(1/4): c7-media/group_gz                                                                                                  | 166 kB  00:00:00     
(2/4): c7-media/filelists_db                                                                                              | 3.2 MB  00:00:00     
(3/4): c7-media/primary_db                                                                                                | 3.1 MB  00:00:00     
(4/4): c7-media/other_db                                                                                                  | 1.3 MB  00:00:00     
元数据缓存已建立
[root@iKuaiOS ~]# ./case3.sh 
1.配置本地源
2.配置网络源
3.配置selinux状态
4.修改网卡模式
请输入选项:2
网络源文件配置成功-----
正在关闭本地源----
2022年 10月 08日 星期六 19:24:38 CST本地源关闭成功----
[root@iKuaiOS ~]# ./case3.sh 
1.配置本地源
2.配置网络源
3.配置selinux状态
4.修改网卡模式
请输入选项:3
请选择selinux状态(enforcing|disabled):disabled
修改成功------
     7	SELINUX=disabled
[root@iKuaiOS ~]# ./case3.sh 
1.配置本地源
2.配置网络源
3.配置selinux状态
4.修改网卡模式
请输入选项:3
请选择selinux状态(enforcing|disabled):enforcing
修改成功------
     7	SELINUX=enforcing
[root@iKuaiOS ~]# ./case3.sh 
1.配置本地源
2.配置网络源
3.配置selinux状态
4.修改网卡模式
请输入选项:4
请选择ip获取方式[d(dhcp)/s(static)]:s
请输入ip:192.168.18.134
是否使用默认netmask为255.255.255.0(y/n):y
是否使用默认网关为192.168.18.2(y/n):y
是否使用默认DNS114.114.114.114(y/n):y
请选择是否开机自动配ip(y/n):y
请注意------重启网卡后,当前终端可能会断开-------
是否重启网卡(y/n):y
您的IP是:192.168.18.134
正在重启网卡-----
如果远程连接断开,请根据上面IP重新远程连接-----------------