大家好,我是云计算磊哥,从业20年的IT老鸟。运维培训15年,总结了一套从入门到精通的全运维开发宝典手册。准备用300天时间写一套博文,手把手从安装软件讲起,从linux系统管理,shell脚本编程,mysql运维架构备份核心技术,Apache/nginx/tomcatWEB服务器管理,ansible自动化运维,redis集群哨兵,LVM/HAproxy/keepalived集群架构,rabbitMQ消息队列,docker&K8S集群资源管理,K8S自愈,K8S自动扩容,PYTHON编程,PYTHON自动化运维,从行业到产品,从过去到未来,从理论到操作,从视频到文档工具,100+篇系列文章一站式发布。从零基础入门到30k运维开发工程师岗位诸多就业问题。多方位全方面的给你讲清楚云计算这个行业该如何做。关注我。后续AI大模型开发课程更精彩。

Shell编程-流程控制

1.Shell 条件测试

格式1: test 条件表达式
格式2: [ 条件表达式 ]
格式3: [[ 条件表达式 ]]

1.1文件测试
[root@xulei ~]# test -d /home
[root@xulei ~]# echo $?
0
[root@xulei ~]# test -d /home11111
[root@xulei ~]# echo $?
1
[root@xulei ~]# [ -d /home ]
[ -e dir|file ]
[ -d dir ]
[ -f file ]		是否存在,而且是文件
[ -r file ]		当前用户对该文件是否有读权限
[ -x file ]
[ -w file ]
[ -L file ]
[root@xulei ~]# [ ! -d /ccc ]  && mkdir /ccc
[root@xulei ~]# [ -d /ccc ]  || mkdir /ccc
1.2数值比较
[ 1 -gt 10 ]	大于
[ 1 -lt 10 ]	小于
[ 1 -eq 10 ]	等于
[ 1 -ne 10 ]  	不等于
[ 1 -ge 10 ]	大于等于
[ 1 -le 10 ]	小于等于
[root@xulei ~]# disk_use=$(df -P |grep '/$' |awk '{print $5}' |awk -F% '{print $1}')
[root@xulei ~]# [ $disk_use -gt 90 ] && echo "war......"
[root@xulei ~]# [ $disk_use -gt 60 ] && echo "war......"
war......

[root@xulei ~]# id -u
0
[root@xulei ~]# [ $(id -u) -eq 0 ] && echo "当前是超级用户"
当前是超级用户
[alice@xulei ~]$ [ $UID -eq 0 ] && echo "当前是超级用户" || echo "you不是超级用户"
you不是超级用户
1.3C语言风格的数值比较
[root@xulei ~]# ((1<2));echo $?
0
[root@xulei ~]# ((1==2));echo $?
1
[root@xulei ~]# ((1>2));echo $?
1
[root@xulei ~]# ((1>=2));echo $?
1
[root@xulei ~]# ((1<=2));echo $?
0
[root@xulei ~]# ((1!=2));echo $?
0
[root@xulei ~]# ((`id -u`>0));echo $?
1
[root@xulei ~]# (($UID==0));echo $?
0
1.4字符串比较
提示:使用双引号
[root@xulei ~]# [ "$USER" = "root" ];echo $?
0
[root@xulei ~]# [ "$USER" == "root" ];echo $?
0

[root@xulei ~]# BBB=""
[root@xulei ~]# echo ${#BBB}
0
[root@xulei ~]# [ -z "$BBB" ]	字符长度是为0
[root@xulei ~]# echo $?
0
[root@xulei ~]# [ -n "$BBB" ]	字符长度不为0
[root@xulei ~]# echo $?
1
2.shell分支if语句
2.1if语句结构
1.单分支结构
if 条件测试
then 命令序列
fi
2.双分支结构
if 条件测试
then 命令序列
else 命令序列
fi
3.多分支结构
if 条件测试1
then 命令序列

[elif 条件测试2
then 命令序列

elif 条件测试3 
then 命令序列]...

else 命令序列
fi

2.1if语句实例
read -p "确认开始安装KVM [y]: " kvm_install
if [ ! "${kvm_install}" = "y" ];then
         echo -e "$red_col输入不正确! $reset_col"
         exit
fi
3.Shell分支case语句

3.1case语法结构

case 变量 in
模式1)
	命令序列1
	;;
模式2)
	命令序列2
	;;
模式3)
	命令序列3
	;;
*)
	无匹配后命令序列
esac
3.2case案例
案例1:简单的模式匹配
确定要继续删除吗 yes/no: "  y
案例2:系统管理工具箱
Command action
	h	显示命令帮助
	f	显示磁盘分区
	d	显示磁盘挂载
	m	查看内存使用
	u	查看系统负载
	q	退出程序
Command (h for help): m
                   total       used       free      shared    buffers     cached
Mem:          7628        840       6788          0         29        378
Swap:         2047          0         2047
案例3:简单的JumpServer

		跳板主机
		1)mysql1
		2)mysql2
		3)bj-web1
		h)  help
		q)  exit
请选择要连接的主机[1-3]: 1
Last login: Sun Sep  6 04:18:01 2025 from 192.168.122.1
[xulei@xulei1 ~]$ ip a show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 52:54:00:ea:e7:d1 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.186/24 brd 192.168.122.255 scope global eth0
    inet6 fe80::5054:ff:feea:e7d1/64 scope link 
       valid_lft forever preferred_lft forever
4.Shell编程之循环结构
4.1shell循环for语句
1.for 语法结构
Shell:
for 变量名 [ in 取值列表 ]
do
	循环体
done

C语言:
for ((初值;条件;步长))
do	
	循环体
done
4.2shell循环while语句
1.while语句结构
while 条件测试
do
	循环体
done
当条件测试成立(条件测试为真),执行循环体
4.3shell循环until
2.until语法结构
until 条件测试
do
	循环体
done
当条件测试成立(条件测试为假),执行循环体
4.3shell循环控制
1.break:
break命令允许跳出所有循环(终止执行后面的所有循环)。
2.continue:
continue命令与break命令类似,只有一点差别,它不会跳出所有循环,仅仅跳出当前循环。
3.exit:
exit用于在程序运行的过程中随时结束程序,exit的参数是返回给OS的。
4.shift:
位置参数可以用shift命令左移。比如shift 3表示原来的$4现在变成$1,原来的$5现在变成$2等等,原来的$1$2$3丢弃,$0不移动。不带参数的shift命令相当于shift 1

配套视频教材

https://edu.51cto.com/course/39717.html

https://edu.csdn.net/course/detail/40863

Logo

openEuler 是由开放原子开源基金会孵化的全场景开源操作系统项目,面向数字基础设施四大核心场景(服务器、云计算、边缘计算、嵌入式),全面支持 ARM、x86、RISC-V、loongArch、PowerPC、SW-64 等多样性计算架构

更多推荐