今天我们继续学习Linux中的shell脚本流程控制内容

发布于:2025-09-05 ⋅ 阅读:(19) ⋅ 点赞:(0)

1.条件判断case语句


case语句的基本格式:

#!/bin/bash
#case $1 in
#[0-9])
#       command1
#;;
#[a-z])
#       command2
#;;
#*)
#       command3
#;;
#esac

#案例1:条件1【0-9】输出是数字;条件2【a-z】输出是字母;不满足以上条件
时,输出“noway”。

[root@localhost ~]# vim case.sh
case $1 in
[0-9])
        echo "number"
;;
[a-z])
        echo "letter"
;;
*)
        echo "noway"
;;
esac
[root@localhost ~]# chmod +x case.sh
[root@localhost ~]# ./case.sh 1
number
[root@localhost ~]# ./case.sh a
letter
[root@localhost ~]# ./case.sh aasd..
noway

案例:对nginx服务写启停脚本

vim /etc/init.d/nginxd
#!/bin/bash
#chkconfig: 35 85 19
#c2508
#20250904
#nginx start or stop script
case $1 in
start)
        status1=$(netstat -anptu | grep [n]ginx | wc -l)
        if [ $status1 -ge 1 ];then
                echo "Nginx is running!"
        else
                nginx
        fi
;;
stop)
        status1=$(netstat -anptu | grep [n]ginx | wc -l)
        if [ $status1 -eq 0 ];then
                echo "Nginx is stopped!"
        else
                nginx -s stop
        fi
;;
reload)
        nginx -s reload
;;
restart)
        status1=$(netstat -anptu | grep [n]ginx | wc -l)
        if [ $status1 -eq 0 ];then
                echo "Nginx is stopped!"
        else
                nginx -s stop
        fi
        sleep 2
        nginx
;;
status)
        netstat -anptu | grep [n]ginx
;;
*)
        echo "USAGE: $0 start | stop | reload "
;;
esac
[root@localhost ~]# service nginxd start
Nginx is running!
[root@localhost ~]# service nginxd stop
[root@localhost ~]# service nginxd stop
Nginx is stopped!
[root@localhost ~]# service nginxd restart
Nginx is stopped!
[root@localhost ~]# service nginxd start
Nginx is running!
[root@localhost ~]# service nginxd status
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      30783/nginx: master 
tcp6       0      0 :::80                   :::*                    LISTEN      30783/nginx: master 
[root@localhost ~]# service nginxd stop
[root@localhost ~]# service nginxd status
[root@localhost ~]# service nginxd stop
Nginx is stopped!

case支持glob风格的通配符

*: 任意长度任意字符
?: 任意单个字符
[]: 指定范围内的任意单个字符
|: 或,如a|b ,a或b

注意:case不支持正则表达式。

示例:工作选项

[root@localhost ~]# vim tbjh.sh
#!/bin/bash
cat <<EOF
请选择
1.备份文件
2.清理日志文件
3.软件升级
4.软件回滚
5.删库跑路
EOF
read -p "请选择工作类型:" n1
case $n1 in
1)
        echo "备份文件!"
        $0
;;
2)
        echo "清理日志文件!"
        $0
;;
3)
        echo "软件升级!"
        $0
;;
4)
        echo "软件回滚!"
        $0
;;
5)
        echo "删库跑路!"
        $0
;;
q)
        exit
;;
*)
        echo "USAGE: 必须输入菜单中的数字!"
        $0
;;
esac

2.循环


循环执行介绍

将某代码段重复运行多次,通常有进入循环的条件和退出循环的条件

重复运行次数

  • 循环次数事先已知

  • 循环次数事先未知

常见的循环的命令:for,while

#循环的逻辑:程序先进行语句判断,如果为真则执行循环语句,然后再进行语句判断,直至语句判断失败才跳出

2.1for循环

格式:

# 第一种写法
for NAME [in words ...]; do commands;done

# 第二种写法
for 变量 in 列表
    循环体
done

# 第三种写法
for 变量 in 列表
do
    循环体
done

示例:打印99乘法表

#!/bin/bash
for i in `seq 1 9`
do
        for j in `seq 1 $i`
        do
                echo -e -n "$j x $i = $[i*j]\t"
        done
        echo
done
for ((i=1;i<=9;i++))
do
        for ((j=1;j<=$i;j++))
        do
                echo -e -n "$j x $i = $[i*j]\t"
        done
        echo
done

2.2while循环

格式:

#!/bin/bash
#i=1
#while [ i -le 10]
#do
#       echo $i
#       let i++
#done

示例:

#!/bin/bash
while [ $i -le 10 ]#!/bin/bash
do
        echo $i
        let i++
done
[root@localhost ~]# ./while.sh 
1
2
3
4
5
6
7
8
9
10

for循环与while循环的区别,for的底层代码其实就是while。

#!/bin/bash
i=1
for ((;i<=10;))
do
        echo $i
        let i++
done

无限循环

while true;do
    循环体
done

玩个猜数游戏(放松一下)

#!/bin/bash
price=$[RANDOM%1000]
times=0
while true
do
        read -p "请输入你猜测的价格[0 ~ 999]:" guess
        let times++
        echo $times
        if [ $guess -eq $price ];then

                if [ $times -le 10 ];then
                        echo "搞得不错啊,兄弟$times次就猜出来了!"
                else
                        echo "搞得一般化,要猜$times次吗算?"
                fi
                exit
        elif [ $guess -lt $price ];then
                echo "猜小啦!"
        else
                echo "猜大啦!"
        fi
done


网站公告

今日签到

点亮在社区的每一天
去签到