当前位置: 首页> 文旅> 美景 > Linux5:Shell编程——流程控制

Linux5:Shell编程——流程控制

时间:2025/8/26 20:01:40来源:https://blog.csdn.net/weixin_65047977/article/details/140897480 浏览次数:0次

目录

前言

一、if-else

二、for循环

三、while语句、无限循环和until循环

1.while语句

2.无限循环

3.until循环

四、case ... esac

五、跳出循环

1.break

2.continue循环

总结


前言

        Shell流程控制包括:if-else语句、for循环、while语句、无限循环、until循环、case ... esac和跳出循环。

 

一、if-else

  • if结构的几种写法
# if 控制语句# if判断
# if command;
# then
#     code
# fi# if else 语句 
# if command;
# then
#     code
# else
#     code
# fi# if elif else 语句
# if command;
# then   
#     code
# elif command2;
# then
#     code
# *n
# else
#     code
# fi
  • 举例
#!/bin/shif [ `ps -ef | grep -c "ssh"` -gt 1 ];   # ps -ef 列出当前系统中所有的进程信息。# grep -c "ssh" 统计这些进程信息中包含字符串 "ssh" 的行数。
thenecho "True"
fiecho "-----------------"
if ((10>29));
then echo "10 bigger then 20"
elseecho "10 less then 20"
fiecho "------------------"
# elif
a=10
b=20
if [ $a -eq $b ];
then echo "a=b"
elif [ $a -gt $b ];
thenecho  "a>b"
else [ $a -lt $b ];echo "a<b"
fi
  • 输出
True
-----------------
10 less then 20
------------------
a<b

 

二、for循环

  • 结构
# for循环
# for i in xxx;
# do 
#     code
# done
  • 举例
#!/bin/shfor i in "I\'m good at coding";
do echo $i
donefor i in 1 2 3 4 5 ;
do echo the value is $i
donefor i in "$@";         # 文件传参 $@ 循环获取传入的参数
do echo $i
done
  • 输出
[root@tokyo001 shell_code]# sh demo10.sh 2 3 4 5 5  # 文件传参写法
I\'m good at coding
the value is 1
the value is 2
the value is 3
the value is 4
the value is 5
2
3
4
5
5

 

三、while语句、无限循环和until循环

1.while语句

  • 结构
# while循环
# while codition
# do 
#     code
# done
  • 举例:注意这里出现了shell编程里的自增语句 let "n++"
#!/bin/sha=1
while [ $a -le 5 ]
do echo $alet "a++"   # 等价于 a=`expr $a + 1 `
done
  • 输出
1
2
3
4
5

 

2.无限循环

  • 结构
#无限循环语法格式
# while :   # 或者 while True
# do 
#     echo "im handsome"
# done# for ((;;))
  • 举例
#!/bin/sh# 死循环
echo "请按ctrl+c结束"
echo -n "请输入你喜欢的电影:"     # -n 不要在输出末尾添加换行符
while read film
do echo "${film}是一部好电影"
done
  • 输出:程序无限循环 按ctrl + c可退出
请按ctrl+c结束
请输入你喜欢的电影:hhh
hhh是一部好电影
xxxx
xxxx是一部好电影
gggg
gggg是一部好电影

 

3.until循环

  • 举例
#!/bin/sh# until循环 直到条件为真为止
a=0
until [ ! $a -lt 10 ]
do echo $alet "a++"
done
  • 输出
0
1
2
3
4
5
6
7
8
9

 

四、case ... esac

  • 举例
#!/bin/sh
# case esac  模式匹配echo "请输入1-4之间的数字"
read -p "请输入数字:" num
case $num in1)echo "you choose 1";;   # 表示匹配结束2)echo "you choose 2";;3)echo "you choose 3";;4)echo "you choose 4";;*)   # 8 通配其他结果echo "you choose others";;  
esacecho "----------------"
a="hhh"
case $a in "hhh")echo "you are hhhhhhhhh";;"xxx")echo "you are xxxxxxxxx";;"ggg")echo "you are gggggggggg";;
esac
  • 输出:一种是获取用户输入的数据进行判断,一种是直接判断
请输入1-4之间的数字
请输入数字:2
you choose 2
----------------
you are hhhhhhhhh

 

五、跳出循环

1.break

  • break 命令允许跳出所有循环(终止执行后面的所有循环)
  • 举例
#!/bin/sh# break 跳出循环
echo "welcome to paris"
echo "plese tell me yuor age"
while :
do read -p "plese tell me yuor age of ten " agecase $age in1|2|3|4)echo "your are so yong";;*)echo "old man"break          # 跳出整个循环;;esac
done
  • 输出
welcome to paris
plese tell me yuor age
plese tell me yuor age of ten 1
your are so yong
plese tell me yuor age of ten 1
your are so yong
plese tell me yuor age of ten 1
your are so yong
plese tell me yuor age of ten 5
old man

 

2.continue循环

  • continue 命令,仅跳出当前循环
  • 举例
#!/bin/sh# continue 跳出循环
echo "welcome to paris"
echo "plese tell me yuor age"
while :
do read -p "plese tell me yuor age of ten " agecase $age in1|2|3|4)echo "your are so yong";;*)echo "old man"continue        # 跳出当前循环;;esac  
done
  • 输出:用户输入1,2,3,4以外的数字仍不会跳出整个循环,只跳出了这一次的循环
welcome to paris
plese tell me yuor age
plese tell me yuor age of ten 1
your are so yong
plese tell me yuor age of ten 2
your are so yong
plese tell me yuor age of ten 3
your are so yong
plese tell me yuor age of ten 1
your are so yong
plese tell me yuor age of ten 6
old man
plese tell me yuor age of ten 6
old man
plese tell me yuor age of ten 

 

总结

        Shell流程控制包括:if-else语句、for循环、while语句、无限循环、until循环、case ... esac和跳出循环。

        Shell流程控制包括:if-else语句、for循环、while语句、无限循环、until循环、case ... esac和跳出循环。

        重要的事情说三遍哈哈哈哈。

关键字:Linux5:Shell编程——流程控制

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: