shell脚本学习之基础篇四:循环(for、while、until)及循环控制语句(continue、break)

一、for循环

1、语法结构

(1)列表循环

用于将一组命令,执行已知的次数

for 变量名 in {list}
do
    command
done
for 变量名 in a b c
do
    command
done

例1:打印1-5这5个数字

[root@server ~]# vim for.sh

#!/bin/bash

for i in {1..5}
  do
        echo $i
  done
  
[root@server ~]# . for.sh 
1
2
3
4
5

例2:打印5次hello world
注意:虽然我们定义了一个变量i,但是没有使用它,它只是控制循环次数

[root@server ~]# vim for.sh

#!/bin/bash

for i in {1..5}
  do
        echo hello world
  done
  
[root@server ~]# . for.sh 
hello world
hello world
hello world
hello world
hello world

例3:打印abcde

[root@server ~]# vim for.sh

#!/bin/bash

for i in a b c d e
  do
        echo $i
  done

[root@server ~]# . for.sh 
a
b
c
d
e

例4:输出0-50之间的偶数

[root@server ~]# vim for1.sh

#!/bin/bash
for i in {0..50..2}  //..2代表步长为2,每隔2个
do
        echo $i
done

[root@server ~]# . for1.sh 
0
2
4
6
8
……
48
50

附1小技巧:花括号{}和seq在for循环的应用:

for i in {1..50..2} 1-50的奇数

for i in {2..50..2} 1-50的偶数

for i in {10..1}  1-10倒序排列

for i in $(seq 10)  1-10正序排列

for i in $(seq 10 -1 1) 1-10倒序排列

for i in $(seq 1 2 10) 1-10的奇数,中间为步长

(2)不带列表循环

不带列表循环执行时由用户指定参数和参数的个数决定的

for 变量名 
do
    command
done

例1:打印hello

[root@server ~]# vim for2.sh 

#!/bin/bash
for i
do
        echo hello
done

[root@server ~]# . for2.sh   //没有给脚本传参所以执行了没有结果

[root@server ~]# . for2.sh a   //把a赋值给变量i,i有值了它就开始执行do..done了
hello
[root@server ~]# . for2.sh a b
hello
hello
[root@server ~]# . for2.sh a b 1   //传什么值不重要,传了3次它就执行了3次
hello
hello
hello

(3)类C风格的for循环

for ((expr1;expr2;expr3))
do
       command
done

expr1:定义变量并赋初值
expr2:决定是否循环
expr3:决定循环变量如何改变,决定循环什么时候退出

例1:打印1-5

扫描二维码关注公众号,回复: 11469389 查看本文章
[root@server ~]# vim for3.sh
#!/bin/bash
for ((i=1;i<=5;i++))
do
     echo $i
done

例2:打印1-10的奇数

[root@server ~]# vim for3.sh
#!/bin/bash
for ((i=1;i<=10;i+=2))   //i=i+2
do
     echo $i
done

[root@server ~]# . for3.sh 
1
3
5
7
9

附2:类C风格运算符用法

++ 自身变量+1
自身变量-1
+=5 自身变量+5
-=5 自身变量-5
*=5 自身变量*5
/=5 自身变量/5
%=5 自身变量%5

例3:计算1-100的奇数和

[root@server ~]# vim for3.sh 

#!/bin/bash
sum=0
for ((i=1;i<=100;i+=2))
do
     let sum=$i+$sum
done
echo "1-100的奇数和为:$sum"

[root@server ~]# . for3.sh 
1-100的奇数和为:2500

二、while循环

while循环一般用于有条件判断的循环,若判断条件为真,则进入循环,当条件为假就跳出循环

1、语法结构

while 表达式
do
    command
done

例1:打印1-5

[root@server ~]# vim while.sh
#!/bin/bash
i=1
while [ $i -le 5 ]
do
    echo $i
    let i++
done

[root@server ~]# . while.sh 
1
2
3
4
5

例2:输出1-100之间不能被3整除的数字

[root@server myscripts]# vim 33.sh 

#!/bin/bash
i=1
while [ $i -le 100 ]
 do
 if [[ $i%3 -ne 0 ]]
 then echo "$i"
 fi
 let i++
done

[root@server myscripts]# . 33.sh 
1
2
4
5
7
8
10
……

2、死循环

(1)语法结构(3种)

while [ 1 -eq 1 ]  //写一个永远为真的表达式,1等于1这个条件永远为真,所以这个脚本会一直循环下去
do
    command
done
while true
do
    command
done
while :
do
    command
done

例1:每2秒跟ntp服务器同步一下系统时间,同步失败立即给管理员发邮件,同步成功每2次才给管理员发一次邮件

[root@server ~]# vim ntpdate.sh 

#!/bin/bash
ntp=192.168.100.120  //局域网中的ntp服务器地址
count=0
while true 
do
ntpdate $ntp &>/dev/null  //同步时间用ntpdate命令
if [ $? -ne 0 ]
  then
    echo "ntp failed!" | mail -s "check system date" root@server
  else
    let count++   //同步成功一次count值就加1
    if [ $count -eq 2 ]  //等于2次就发送一次成功邮件
      then
        echo "ntp success!" | mail -s "system date is success" root@server && count=0   //不希望count越来越大所以每次都重置为0
    fi
fi
sleep 2  //每隔2s同步一次时间
done
[root@server ~]# tail -f /var/spool/mail/root    //查看管理员邮件
From [email protected]  Sun Jul 26 17:27:45 2020
Return-Path: <[email protected]>
X-Original-To: root@server
Delivered-To: [email protected]
Received: by server.localdomain (Postfix, from userid 0)
	id 0900630188127; Sun, 26 Jul 2020 17:27:45 +0800 (CST)
Date: Sun, 26 Jul 2020 17:27:45 +0800
To: [email protected]
Subject: system date is success
User-Agent: Heirloom mailx 12.5 7/5/10
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Message-Id: <[email protected]>
From: [email protected] (root)

ntp success!

三、until循环

跟while相反,条件为假进入循环,条件为真退出循环

1、语法结构

until 表达式
do
    command
done

例1:打印1-5

[root@server ~]# vim until.sh
#!/bin/bash
i=1
until (($i > 5))
do
    echo $i
    let i++
done

[root@server ~]# . until.sh 
1
2
3
4
5
[root@server ~]# vim until1.sh
#!/bin/bash
i=5
until (($i < 1))
do
    echo $i
    let i--
done

[root@server ~]# . until1.sh 
5
4
3
2
1

例2:计算1-50的和

[root@server myscripts]# vim 50.sh
#!/bin.bash
i=1
sum=0
until [ $i -eq 51 ]   //这里注意如果是50的话条件为真循环结束它计算的是1-49的和
  do
  sum=$[$i+$sum]
  let i++
  done
echo "$sum"

[root@server myscripts]# . 50.sh 
1275

2、死循环结构

until false
do
    command
done
until [ 1 -ne 1 ]
do
    command
done

四、循环控制语句

for循环一般会搭配条件判断语句和流程控制语句一起执行,那么就会出现需要跳过循环和中止循环的情况,控制循环的命令有以下3个:

1、continue

继续,但不会执行循环体内下面的代码了,开始重新开始下一次循环

例1:打印1-10的数字,3不打印

[root@server ~]# vim for4.sh

#!/bin/bash
for ((i=1;i<=10;i++))
do
        if [ $i -eq 3 ];then
        continue
        else
        echo $i
        fi
done

[root@server ~]# . for4.sh 
1
2
4
5
6
7
8
9
10

2、break

打断,马上停止本次循环,执行循环体外的代码

例2:1-10的数字,7后面的都不打印

[root@server ~]# vim for4.sh

#!/bin/bash
for ((i=1;i<=10;i++))
do
        if [ $i -eq 8 ];then
        break
        else
        echo $i
        fi
done

[root@server ~]# . for4.sh 
1
2
3
4
5
6
7

3、exit

直接跳出程序

猜你喜欢

转载自blog.csdn.net/shengjie87/article/details/107586521