bash中有for、while、until三种循环结构;for循环次数一般是固定的,while和until的循环次数可以是固定也可以是不固定的。
1、for循环:
for循环次数是固定的
1.1、语法结构:
(1)、
for 变量名 [ in 取值列表]
do
command1;
command2;
...
done
or 可以写到一行:
for 变量名 [ in 取值列表 ] ; do command1; command2... done;
(2)、
for ((初值;条件;不长))
do
command1;
command2;
...
done
1.2、实例:
seq 命令: 生成数字序列
[root@centos7 scripts]# cat for_sum.sh
#!/usr/bin/bash
#sum求和
#20191210 by zhaoyq
for i in {1..100}
do
let sum+=$i
done
echo "sum= $sum"
[root@centos7 scripts]#
[root@centos7 scripts]#
[root@centos7 scripts]#
[root@centos7 scripts]# ./for_sum.sh
sum= 5050
[root@centos7 scripts]#
[root@centos7 scripts]# cat for_sum2.sh
#!/usr/bin/bash
#sum求和
#seq 序列
#v1.0 20191210 by zhaoyq
for i in `seq $1`
do
let sum+=$i
done
echo "sum= $sum"
[root@centos7 scripts]#
[root@centos7 scripts]# ./for_sum2.sh 100
sum= 5050
[root@centos7 scripts]#
[root@centos7 scripts]# cat user.txt
zhangsan
lisi
wangwu
zhaoliu
[root@centos7 scripts]#
[root@centos7 scripts]# cat read_for.sh
#!/usr/bin/bash
#按行循环读取文件内容
#v1.0 by zhaoyq 20191210
for user in `cat user.txt`
do
echo $user
done
[root@centos7 scripts]#
[root@centos7 scripts]# ./read_for.sh
zhangsan
lisi
wangwu
zhaoliu
[root@centos7 scripts]#
##修改user.txt文件使姓名用空格分开
[root@centos7 scripts]# vim user.txt
zhang san
li si
wang wu
zhao liu
##再执行read_for.sh 脚本,发现输出结果不在按行输出,而是姓名分开输出
[root@centos7 scripts]# ./read_for.sh
zhang
san
li
si
wang
wu
zhao
liu
[root@centos7 scripts]#
##这时需要修改分隔符为换行符
[root@centos7 scripts]# cat ./read_for2.sh
#!/usr/bin/bash
#按行循环读取文件内容
#v2.0 by zhaoyq 20191210
##修改分割符为换行符
#IFS=$'\n'
IFS='
'
for user in `cat user.txt`
do
echo $user
done
[root@centos7 scripts]#
[root@centos7 scripts]# ./read_for2.sh
zhang san
li si
wang wu
zhao liu
[root@centos7 scripts]#
[root@centos7 scripts]# cat ./read_for2.sh
#!/usr/bin/bash
#按行循环读取文件内容
#v2.0 by zhaoyq 20191210
##修改分割符为换行符
IFS=$'\n'
#IFS='
#'
for user in `cat user.txt`
do
echo $user
done
[root@centos7 scripts]#
[root@centos7 scripts]# ./read_for2.sh
zhang san
li si
wang wu
zhao liu
[root@centos7 scripts]#
##下面for1.sh和for2.sh效果是一样的,for i 等价于 for i in "$@" 或 for i in $*
[root@centos7u7 scripts]# cat for1.sh
#!/bin/bash
for i
do
let sum+=$i
done
echo $sum
[root@centos7u7 scripts]#
[root@centos7u7 scripts]#
[root@centos7u7 scripts]# cat for2.sh
#!/bin/bash
for i in "$@"
do
let sum+=$i
done
echo $sum
[root@centos7u7 scripts]#
[root@centos7u7 scripts]# ./for1.sh 1 2 3 4
10
[root@centos7u7 scripts]#
[root@centos7u7 scripts]# ./for2.sh 1 2 3 4
10
[root@centos7u7 scripts]#
[root@centos7u7 scripts]# ./for2.sh 1 2 3 4 5
15
[root@centos7u7 scripts]#
[root@centos7u7 scripts]# ./for1.sh 1 2 3 4 5
15
[root@centos7u7 scripts]#
小结:for循环的循环次数一般是固定的;在读取文件时,默认是按空格、tab键分割元素,如果需要按行读取,则需要定义分隔符IFS。
2、while循环:
2.1、语法结构:
while 条件测试
do
command1
command2
...
done
##条件测试为真时执行循环
2.2、实例:
[root@centos7 scripts]# cat while_sum.sh
#!/usr/bin/bash
#while循环sum求和
#v1.0 20191210 by zhaoyq
i=1
while [ $i -le 100 ]
do
let sum+=$i
let i++
done
echo "sum= $sum"
[root@centos7 scripts]#
[root@centos7 scripts]# ./while_sum.sh
sum= 5050
[root@centos7 scripts]#
[root@centos7 scripts]# cat while_read.sh
#!/usr/bin/bash
#while按行循环读取文件内容
#v1.0 by zhaoyq 20191210
#for user in `cat user.txt`
while read user
do
echo $user
done < user.txt
[root@centos7 scripts]#
[root@centos7 scripts]# while_read.sh
zhang san
li si
wang wu
zhao liu
[root@centos7 scripts]#
[root@centos7 scripts]# cat while_ping.sh
#!/usr/bin/bash
#while循环ping主机检测
#v1.0 by zhaoyq 20191210
ip=$1
## : 可以替换为 true
while :
do
ping -c1 -W1 $ip &> /dev/null
if [ $? -ne 0 ];then
echo "$ip is down!"
else
sleep 2
echo "$ip is up!"
fi
done
[root@centos7 scripts]#
[root@centos7 scripts]# ./while_ping.sh 192.168.86.130
192.168.86.130 is up!
192.168.86.130 is up!
192.168.86.130 is down!
192.168.86.130 is down!
192.168.86.130 is down!
3、until 循环
3.1、语法结构:
until 条件测试
do
command1
command2
...
done
##条件测试为假时执行循环
3.2、实例:
[root@centos7 scripts]# cat until_sum.sh
#!/usr/bin/bash
#until循环sum求和
#v1.0 20191210 by zhaoyq
i=1
until [ $i -gt 100 ]
do
let sum+=$i
let i++
done
echo "sum= $sum"
[root@centos7 scripts]#
[root@centos7 scripts]# ./until_sum.sh
sum= 5050
[root@centos7 scripts]#
[root@centos7 scripts]# cat until_ping.sh
#!/usr/bin/bash
#until循环ping主机检测
#v1.0 by zhaoyq 20191210
ip=$1
#while :
until false
do
ping -c1 -W1 $ip &> /dev/null
if [ $? -ne 0 ];then
echo "$ip is down!"
else
sleep 2
echo "$ip is up!"
fi
done
[root@centos7 scripts]#
[root@centos7 scripts]# ./until_ping.sh 192.168.86.130
192.168.86.130 is up!
192.168.86.130 is up!
192.168.86.130 is up!
192.168.86.130 is down!
192.168.86.130 is down!
192.168.86.130 is down!
4、强制跳出循环命令:
4.1、break命令:
break强制跳出整个循环
break n 跳出n层循环
4.2、continue命令:
continue命令跳出当前循环
4.3实例:
[root@centos7 scripts]# cat break01.sh
#!/usr/bin/bash
#break命令测试
#v1.0 by zhaoyq 20191210
for i in `seq 10`
do
if [ $i -eq 5 ]
then
break;
fi
echo $i
done
[root@centos7 scripts]#
[root@centos7 scripts]# break01.sh
1
2
3
4
[root@centos7 scripts]# cat continue01.sh
#!/usr/bin/bash
#continue命令测试
#v1.0 by zhaoyq 20191210
for i in `seq 10`
do
if [ $i -eq 5 ]
then
continue;
fi
echo $i
done
[root@centos7 scripts]#
[root@centos7 scripts]# continue01.sh
1
2
3
4
6
7
8
9
10