本节主要对shell中循环的使用,如for、while、until做一个介绍。
一、for循环
语法结构
for 变量 in 变量列表
do
循环体 //可以重复执行n次的
done
说明:
变量列表中的值的数量,决定了循环的次数
[root@shell ~]# vim for1.sh
#!/bin/bash
for i in 1 2 3 haha a b
do
echo $i
done
变量列表的常见写法:
1、直观
a b c d 1 3 5 ... ...
2、使用系统命令
seq 等
[root@shell ~]# vim for2.sh
#!/bin/bash
#for i in `seq 10`
for i in {1..10}
do
echo $i
done
[root@shell ~]# sh for2.sh
1
2
3
4
5
6
7
8
9
10
[root@shell ~]# seq 5 -1 1 //倒序打印
5
4
3
2
1
3、"$*" 和 "$@"
列表 输出
"$*" a b c d "a b c d"
"$@" a b c d "a" "b" "c" "d"
"$#" 显示参数个数
[root@shell ~]# vim for3.sh
#!/bin/bash
for i in "$*"
do
echo $i
done
echo
echo
for i in "$@"
do
echo $i
done
[root@shell ~]# ./for3.sh a b c d
a b c d
a
b
c
d
4、使用变量
[root@shell ~]# vim for4.sh
#!/bin/bash
WORD="q w e r t y u i o p"
for i in $WORD
do
echo $i
done
5.嵌套循环
打印99乘法口诀表
[root@shell ~]# vim 99.sh
#!/bin/bash
for i in `seq 9`
do
for j in `seq $i`
do
echo -n "$j*$i=$[$i*$j] "
done
echo
done
[root@shell ~]# sh 99.sh
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
二、 while语法:
1.变量初始值
while 条件表达式或者命令(read)
do
循环体
变量的更新 //一定要有变量的更新,否则死循环
done
说明:while循环,当判断条件为真时,执行循环体中的操作;当判断条件为假时退出循环。
[root@shell ~]# vim while1.sh
#!/bin/bash
i=1
while [ $i -le 5 ]
do
echo $i
let i++
done
[root@shell ~]# sh while1.sh
1
2
3
4
5
i++:先使用,再加1
++i: 先加1,再使用
2.while与输入重定向结合使用
添加用户的脚本,要求用户的信息来自于文本文件
1)创建用户列表文件
[root@shell ~]# vim userlist
tom:passtom:2017
jack:passjack:2018
mary:passmary:2019
kite:passkite:2020
2)写添加用户的脚本
[root@shell ~]# vim while2.sh
#!/bin/bash
while read line
do
username=`echo $line | cut -d: -f1`
password=`echo $line | cut -d: -f2`
uid=`echo $line | cut -d: -f3`
if id $username &>/dev/null
then
echo "user $username exists."
else
useradd -u $uid $username && echo $password | passwd --stdin $username
fi
done < /root/userlist
[root@shell ~]# sh while2.sh
更改用户 tom 的密码 。
passwd:所有的身份验证令牌已经成功更新。
更改用户 jack 的密码 。
passwd:所有的身份验证令牌已经成功更新。
更改用户 mary 的密码 。
passwd:所有的身份验证令牌已经成功更新。
更改用户 kite 的密码 。
passwd:所有的身份验证令牌已经成功更新。
3.while 死循环
while :
while true
捕捉信号
trap 动作 信号
[root@shell ~]# vim while3.sh
#!/bin/bash
trap "echo You can not kill me." 15
while :
do
echo hello
sleep 2
done
[root@shell ~]# sh while3.sh
hello
hello
hello
hello
... ... //Ctrl+c结束
三、until
语法格式
变量的初始值
until 条件表达式或者命令
do
循环体
变量的更新
done
说明:当条件为真,退出循环
until与while是相反的
[root@shell ~]# vim until.sh
#!/bin/bash
i=1
until [ $i -gt 5 ]
do
echo $i
let i++
done
四、continue:退出本次循环
break:退出当前循环
exit:退出程序
exit 5
[root@shell ~]# vim control.sh
#!/bin/bash
for i in `seq 10`
do
echo $i
done
echo ok
#!/bin/bash
for i in `seq 10`
do
[ $i -eq 5 ] && continue
[-e /etc/passwd ] && continue //遇到i=5的时候,直接结束本次循环,执行下一次循环,即跳过5
echo $i
done
echo ok
#!/bin/bash
for i in `seq 10`
do
[ $i -eq 5 ] && break //遇到i=5的时候,直接结束当前循环,直接执行循环外面的语句
echo $i
done
echo ok
#!/bin/bash
for i in `seq 10`
do
[ $i -eq 5 ] && exit //遇到i=5的时候,直接退出脚本
echo $i
done
echo ok
#!/bin/bash
for i in `seq 10`
do
[ $i -eq 5 ] && exit 5 //exit 执行状态返回值
echo $i
done
echo ok