您的位置:首页 > 运维架构 > Shell

Shell编程之流程控制语句——for循环、while循环

2019-07-18 11:05 615 查看

一、for循环

1、for循环——语法一:

for 变量 in 值1 值2 值3
do
程序
done
  • 每次将值赋给变量后执行程序,每个值都复制一遍在执行;
  • 值与值之间是通过空格来判断的。只要有空格,就认为是一个值;
  • for循环用do和done取代了其他语言中的大括号{};
  • for循环的优点,便于管理员进行系统管理时候,便于简化操作。
  • for循环是这样的,in后面内容是隔开的,不管是空格,还是换行符,里面有几个内容就循环几次。
  • for循环最主要的优势是:事先不需要指定变量的个数,里面有多少个值,他就循环多少次。因此更加方便于系统管理。
  • 示例说明1:
[root@root sh]# pwd
/root/sh
[root@root sh]# vi for1.sh

#!/bin/bash
#打印时间

for time in morning noon afternoon evening
do
echo "This time is $time"
done

#执行结果如下:
[root@root sh]# chmod 755 for1.sh
[root@root sh]# vi for1.sh
[root@root sh]# ./for1.sh
This time is morning
This time is noon
This time is afternoon
This time is evening
  • 示例说明2:批量解压缩脚本
#!/bin/bash
#批量解压缩脚本(事先准备好十几个或二十几个压缩包)

cd /lamp
#将所有的包放到了/root/lamp的目录当中
ls *.tar.gz > ls.log
#压缩包都是以.tar.gz结尾的文件名,并将其写入到 ls.log 的文件(是替换)
for i in $(cat ls.log)
do
tar -zxf $i &>/dev/null
done
rm -rf /lamp/lg.log
  • 示例说明3:批量解压缩脚本
[root@root sh]# ls
case1.sh  case.sh  for1.sh  if1.sh  if2.sh  if4.sh
[root@root sh]# vi for2.sh

#!/bin/bash

cd /root/sh/

ls *.sh > ls.log

y=1
for i in $(cat ls.log)
do
echo $y
y=$(( $y + 1 ))
done

[root@root sh]# chmod 755 for2.sh
[root@root sh]# ./for2.sh
1
2
3
4
5
6
7

2、for循环——语法2:

for (( 初始值;循环控制条件;变量变化 ))
do
程序
done
  • 在Linux的Shell当中,只有用(())双小括号括起来才能进行加减乘除这种数值运算。
  • 示例如下:从1加到100(知道循环次数)
[root@root sh]# vi for3.sh

#!/bin/bash

s=0
for (( i=1;i<=100;i=i+1 ))
do
s=$(( $s+$i ))
done
echo "The sum of 1+2+...+100 is :$s"

[root@root sh]# chmod 755 for3.sh
[root@root sh]# ./for3.sh
The sum of 1+2+...+100 is :5050

注意:
语法一和语法二的区别是是否事先知道循环个数,如果事先知道就执行“ 语法二 ”,否则就执行“ 语法一 ”

  • 示例说明2:批量添加指定数量的用户
[root@root ~]# vim useradd.sh

#Shell脚本如下:
#!/bin/bash
#批量添加指定数量的用户
read -p "Please input user name:" -t 30 name
#输入用户名,等待30s,然后把你输入的值赋值给变量。用户名必须有规律,如,stu0、stu1...
read -p "Please input the number of users:" -t 30 num
#用户的个数
read -p "Please input the password of users:" -t 30 pass
#设置用户密码
if [ ! -z "$name" -a ! -z "$num" -a ! -z "$pass" ]
#判断这个变量里面是否为空。-z 如果为空,输出的值为真; ! -z 如果输入的值不为空,输出的值为真
# -a 是逻辑与
then
y=$(echo $num | sed 's/[0-9]//g')
# sed 's/[0-9]//g 这要我找到了0-9,只要有数字就会将这个变量的结果替换为空。
if [ -z "$y" ]
then
for (( i=1;i<=$num;i=i+1 ))
do
useradd $name$i &>/dev/null
echo $pass | /usr/bin/passwd --stdin $name$i &>/dev/null
done

fi
fi

#结果如下:
[root@root ~]# chmod 755 useradd.sh
[root@root ~]# ./useradd.sh
Please input user name:stu
Please input the number of users:3
Please input the password of users:123
... ...
tcpdump:x:72:72::/:/sbin/nologin
stu1:x:500:500::/home/stu1:/bin/bash
stu2:x:501:501::/home/stu2:/bin/bash
stu3:x:502:502::/home/stu3:/bin/bash

二、while循环与until循环

1、while循环

while循环是 不定循环 ,也称作 条件循环。只要条件判断式成立,循环就会一直继续,直到条件判断是不成立,循环才会停止。这就和for的固定循环不太一样。

格式如下:

while [ 条件判断式 ]
do
程序
done

示例说明:

[root@root ~]# vim while.sh

#!/bin/bash
#从1加到100
i=1
s=0
while [ $i -le 100 ]
#如果变量i 的值小于等于100,则执行循环
do
s=$(( $s+$i ))
i=$(( $i+1 ))
done
echo "The sum is:$s"

[root@root ~]# chmod 755 while.sh
[root@root ~]# ./while.sh
The sum is:5050

2、until循环

until循环,和while循环相反,until循环时只要条件判断式 不成立则进行循环,并执行循环程序。一旦循环条件成立,则终止循环。

示例如下:从1加到100

[root@root ~]# cp while.sh until.sh

#!/bin/bash
i=1
s=0
until [ $i -gt 100 ]
do
s=$(( $s+$i ))
i=$(( $i+1 ))
done
echo "The sum is:$s"

[root@root ~]# vim until.sh
[root@root ~]# ./until.sh
The sum is:5050

总结:

  • while循环、for循环和until循环三种循环其实都是可以相互该改写的。
  • Shell脚本最大的优势是:帮助管理员减少重复操作,或者说帮助管理员进行系统的运维工作;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐