您好,欢迎来到华佗小知识。
搜索
您的当前位置:首页shell脚本中的4种循环语句使用

shell脚本中的4种循环语句使用

来源:华佗小知识
shell脚本中的4种循环语句使⽤

1、for循环

#语法结构#第⼀种:取值变量

for 变量名 in 变量取值表do

指令 done

#例⼦:

#⽰例

for a in {1..9}do

mkdir dir$adone

#第⼆种:C语⾔型for循环

for ((exp1; exp2; exp3))do

指令done

#例⼦:

#⽰例

for ((i=1;i<=3;i++))do

echo $idone

#解释:i从1开始,当i<=3就可以运⾏,如果运⾏的值⼤于3,就退出循环

#语法结构讲解

for关键字后的双括号是三个表达式,

第⼀个是变量初始化(例如:i=1),第⼆个为变量的范围(例如i<=3),第三个为变量⾃增或⾃减(例如i++)。当第⼀个表达式的初始化值符合第⼆个变量的范围时,就进⾏如循环执⾏,当条件不满⾜时就退出循环

#简单⽰例

#1.竖向打印10 9 8 7 6 5⼏个数字#第⼀种⽅法:直接列出元素

[root@game scripts]# cat for1.sh #!/bin/bashfor i in 1 2 3 4 5do

echo $idone

#效果

[root@game scripts]# sh for1.sh 12345

第⼆种⽅法:使⽤⼤括号{}⽣成数字序列

[root@game scripts]# cat for2.sh #!/bin/bashfor i in {1..5}do

echo $idone

#效果

[root@game scripts]# sh for2.sh 12345

#第三种⽅法:使⽤seq⽣成数字序列

[root@game scripts]# cat for3.sh #!/bin/bashfor i in `seq 1 5`do

echo $idone

#效果

[root@game scripts]# sh for3.sh 12345

#2.获取当前⽬录下的⽬录或⽂件名,并将其作为变量列表打印输出

#数据

[root@game ~]# mkdir -p /test/{test1.txt,test2.txt,guo.txt,ke.txt}[root@game ~]# ls -l /test/total 0

drwxr-xr-x. 2 root root 6 Aug 21 22:14 guo.txtdrwxr-xr-x. 2 root root 6 Aug 21 22:14 ke.txtdrwxr-xr-x. 2 root root 6 Aug 21 22:14 test1.txtdrwxr-xr-x. 2 root root 6 Aug 21 22:14 test2.txt#

编写脚本

[root@game scripts]# cat for4.sh #!/bin/bashusage(){

echo \"directory not found\"}

[ ! -d /test ] && usage && exit 1cd /test

for i in `ls`do

echo $idone#效果

[root@game scripts]# sh for4.sh guo.txtke.txttest1.txttest2.txt

2、while循环

#while循环⼀般应⽤于守护进程程序或⼀直循环执⾏#语法格式

while <条件表达式>do

指令done

#简单⽰例

每隔2秒在屏幕上输出⼀次负载值[root@game scripts]# cat while1.sh

#!/bin/bash

while truedo

uptime

sleep 2 #暂停2秒再执⾏done

#提⽰:while true表⽰条件永远为真,因此会⼀直运⾏,像死循环⼀样,称为守护进程#效果:每隔2秒就输出⼀次

[root@game scripts]# sh while1.sh

23:11:35 up 2 days, 2:00, 2 users, load average: 0.00, 0.01, 0.05 23:11:37 up 2 days, 2:00, 2 users, load average: 0.00, 0.01, 0.05 23:11:39 up 2 days, 2:00, 2 users, load average: 0.00, 0.01, 0.05

3、until循环

#until循环是当条件表达式不成⽴时,就会进⼊循环,当条件表达式成⽴时,就会终⽌循环#语法格式

until <条件表达式>do

指令done

#⽰例

#如果⽤户输出的是guoke就符合条件,退出循环,如果不是,⽤户输⼊3次之后就退出循环[root@game scripts]# cat until1.sh#!/bin/bash

i=1

until [ \"$user\" = \"guoke\" -o \"$i\" -gt 3 ]do

read -p \"please enter you username:\" user let i++done

#效果

[root@game scripts]# sh until1.sh please enter you username:guoke[root@game scripts]# sh until1.sh please enter you username:1please enter you username:1please enter you username:1[root@game scripts]#

4、select循环

#语法格式

select 变量名 in [菜单取值列表]do

指令done

#⽰例

#第⼀种:直接使⽤列表字符串

[root@game scripts]# cat select1.sh #!/bin/bash

select name in apache httpd nginx tomcatdo

echo $namedone

#效果

[root@game scripts]# sh select1.sh 1) apache2) httpd3) nginx4) tomcat#? 1

apache#? 3nginx#? 4tomcat#? ^C

#第⼆种:采⽤数组做变量列表

[root@game scripts]# cat select2.sh #!/bin/bash

array=(apache nginx tomcat lighttpd)select name in \"${array[@]}\"do

echo $namedone#效果

[root@game scripts]# sh select2.sh 1) apache2) nginx3) tomcat4) lighttpd#? 3tomcat#? 4lighttpd#? ^C

5.循环控制及状态返回值break (循环控制)continue (循环控制)exit (退出脚本)return (退出函数)#区别

break continue在条件语句及循环语句(for if while等)中⽤于控制程序的⾛向exit是终⽌所有语句并退出脚本

return:仅⽤于在函数内部返回函数执⾏的状态值

#break⽰例

#如果i等于3,那么就终⽌循环

[root@game scripts]# cat break1.sh #!/bin/bash

for ((i=0;i<=5;i++))do

if [ $i -eq 3 ];then break else echo $i fidone

echo \"1111\"

yum install net-tools -y > /dev/null[ $? -eq 0 ] && echo \"already install\"

#效果

[root@game scripts]# sh break1.sh 0121111

already install

#说明:i等于3的时候就终⽌循环,但是没有跳出脚本

#exit⽰例

[root@game scripts]# cat exit1.sh#!/bin/bash

for ((i=0;i<=5;i++))do

if [ $i -eq 3 ];then exit 1 fi

echo $idone

echo \"ok\"

#执⾏效果

[root@game scripts]# sh exit1.sh012

#说明:当i等于3的时候就会退出脚本了,就不会执⾏后⾯的语句

#continue⽰例

[root@game scripts]# cat con1.sh #!/bin/bashfor ((i=0;i<=5;i++))do

if [ $i -eq 3 ];then continue else echo $i fidone

echo \"ok\"

#执⾏效果

[root@game scripts]# sh con1.sh 0

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- huatuo0.cn 版权所有 湘ICP备2023017654号-2

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务