美文网首页
for,while,until

for,while,until

作者: 素瑾汐 | 来源:发表于2017-07-03 23:05 被阅读0次

循环

循环执行

      将某代码重复运行多次
      重复运行多少次:
                循环次数事先已知
                循环次数事先未知
      有进入条件和退出条件

循环执行语句:
for,while,until

for

for

for 变量名 in 列表;do
          循环体
done

执行机制:
依次将列表中的元素赋值给“变量名”;每次赋值后即执行一次循环体;直到列表中的元素耗尽,循环结束

列表生成方式:

      (1)直接给出列表
      (2)整数列表:
                (a){start..end}
                (b)$(seq [start [step]] end)
      (3)返回列表的命令
                $(COMMAND)
      (4)使用glob,如:*.sh
      (5)变量引用:
                $@,$*

例子:

编写一个脚本,ping一个范围内的主机,显示ping结果,并统计ping通的主机有多少台,没ping通的主机有多少台

######方法一:
[root@centos SC]#vim scanip.sh     
#!/bin/bash
net=172.17.252
up=0
down=0
for i in {1..10}
do
        { ping -c1 -W1 $net.$i &> /dev/null; } && { echo $net.$i is up; let ++up; } || { ech
o $net.$i is down;let ++down; }
done
        echo The up host is $up
        echo The down host is $down
~                                                                                           
~                                                                                           
~                                                                                           
~                                                                                           
                                                                                            
▽                                                                                           
~                                                                                           
~                                                                                           
~                                                                                           
~                                                                                           
~                                                                                           
~                                                                                           
"scanip.sh" 10L, 235C written       
######测试结果:                                      
[root@centos SC]#./scanip.sh  
172.17.252.1 is down
172.17.252.2 is down
172.17.252.3 is down
172.17.252.4 is down
172.17.252.5 is down
172.17.252.6 is down
172.17.252.7 is down
172.17.252.8 is down
172.17.252.9 is down
172.17.252.10 is down
The up host is 0
The down host is 10
######方法二:
[root@centos SC]#vim scanip1.sh     
#!/bin/bash
net=192.168.227
up=0
down=0
for i in {128..138}
do
        if ping -c1 -W1 $net.$i &> /dev/null; then
                echo $net.$i is up
                let up++ ;
        else
                echo $net.$i is down
                let down++ ;
        fi
done
echo The up host is $up
echo The down host is $down 
~                                                                                           
~                                                                                           
~                                                                                           
~                                                                                           
~                                                                                           
~                                                                                           
~                                                                                           
"scanip1.sh" 16L, 248C written     
######测试结果:                                       
[root@centos SC]#./scanip1.sh       
192.168.227.128 is down
192.168.227.129 is down
192.168.227.130 is up
192.168.227.131 is down
192.168.227.132 is up
192.168.227.133 is down
192.168.227.134 is down
192.168.227.135 is down
192.168.227.136 is down
192.168.227.137 is down
192.168.227.138 is down
The up host is 2
The down host is 9

特殊用法:
双小括号方法,即((...))格式,也可以用于算术运算
双小括号方法也可以使bash shell实现C语言风格的变量操作

\#I=10
\#((I++))

for循环的特殊格式:

for((控制变量初始化;条件判断表达式;控制变量的修正表达式))
do
          循环体
done

控制变量初始化:仅在运行到循环代码段时执行一次
控制变量的修正表达式:每轮循环结束会先进行控制变量修正运算,而后再做条件判断

[root@centos SC]#sum=0;i=1;while [ "$i" -le 100 ];do let sum+=i i+=2;done;echo sum=$sum     
sum=2500
[root@centos SC]#sum=0;for i in {1..100..2};do let sum+=i;done;echo sum=$sum
sum=2500
[root@centos SC]#for((sum=0,i=1;i<=100;i+=2));do let sum+=i;done;echo sum=$sum               
sum=2500

while

while

while CONDITION ; do
          循环体
done

CONDITION:循环控制条件,进入循环之前,先做一次判断;每一次循环之后会再次做判断;条件为“true”,则执行一次循环;指定条件测试状态为“false”终止循环,因此,CONDITION一般应有循环控制变量;而此变量的值会在循环体不断的被修正
进入条件:CONDITION为true
退出条件:CONDITION为false

示例:编写一个脚本,创建十个用户,并给用户设定八位的随机口令

[root@centos SC]#vim useradd.sh
#!/bin/bash
i=1
while [ "$i" -le 10 ];do
        useradd user$i
        echo "user$i is created"
######生成随机口令
        password=`cat /dev/urandom |tr -dc 'a-zA-Z0-9'|head -c 8`
######给新建用户设定随机口令
        echo $password | passwd --stdin user$i &> /dev/nmll
        let i++
done
~                                                                                     
~                                                                                     
~                                                                                     
~                                                                                     
~                                                                                     
~                                                                                     
~                                                                                     
"useradd.sh" [New] 9L, 209C written                                 
[root@centos SC]#chmod +x useradd.sh
######测试结果
[root@centos SC]#./useradd.sh       
user1 is created
user2 is created
user3 is created
user4 is created
user5 is created
user6 is created
user7 is created
user8 is created
user9 is created
user10 is created
[root@centos SC]#cat /etc/shadow | grep "^user*"
user1:$6$1AhalPjO$W92BskUXlQweC.WT.2jYs90SKm7QbWgBOKbaZ4fWO/n687Kv1t3pRALIYB5nFf9yacJssRGaihMxKhd57U3ag.:17351:0:99999:7:::
user2:$6$fjDqy8Cy$KhwVkLCSWfA.kFDMjQBlOlvfLOVM/C/l/FMyorQtSzadFHv.M1EZl/0atWVkFRx1mkGA6dCf2JfNsMKlndblv/:17351:0:99999:7:::
user3:$6$bW.zRP2R$yBZ7EpHe2dZyUlnRaSjbp69ZoRptdL7QlbHAfvkKEO2FAA3TItf.l5gyzG8Q9PPWNm.MnP7mc.ub/ryr65Zc41:17351:0:99999:7:::
user4:$6$BeboRIh7$XGzKa0FEZ9le5ZoF01yHEGc7McwwHG5fagrfHunloOmcHpv4PYRg9LjwEDd2CmG9vgPecCocf5Akx5aDIY5XY0:17351:0:99999:7:::
user5:$6$QBO0TMwF$kBHG5U7XMd5HhKeEFBtyA9UhCIwUkP7JTVmm2prwjckwIWkyfbt2fPUbcZT7glQR5uzTL3LCC5U1Edb8w9N1O1:17351:0:99999:7:::
user6:$6$9ex4OXkS$2/xeqayIyvq.ByakgGpcwTqhNpSqrPrqZSRrsFpAnVTMwgow4TW7m6XXIchakBYVhVeRVOLiN23jwInGcfbv41:17351:0:99999:7:::
user7:$6$510jI2iP$UKo7QGg2fI3aDPN0k6C7U8Ueoz3SsDsSzlA8uENaApdEuxrsDXwue6LQAlrh8gBUNk.rdtBKnib4PRW0we8xS1:17351:0:99999:7:::
user8:$6$DwD1a33y$mKun3ZQkgf49NGSGIjnD5Ccp1Wx15ar7YBlJwfryVxFIJmLm5HCAaZVhK96KGZdh5v8C4U2WmuIOmA8kAVQjQ0:17351:0:99999:7:::
user9:$6$5mZ3OW1V$/2QlZ6uKbp3AnZxctd7Yxp8SzYTKH8IHSactDJfUwO0vLCtzKIOIv.Td4xFam7mprQG0KhxYy4GWZqEq0trjj0:17351:0:99999:7:::
user10:$6$E8tDCBtU$qVWCI0VsAUxBH1GgsK0E710FzIeX1023050AI//kTNF0Xtpx1N4ENiJnbvXPul7kue9j/hRVPdsG/K2YrrKPB0:17351:0:99999:7:::

while循环的特殊用法(遍历文件的每一行):

while read line; do
          循环体
done < /PATH/FROM/SOMEFILE

依次读取/PATH/FROM/SOMEFILE文件中的每一行,且将 行赋值给变量line

示例:编写一个脚本,读取/etc/passwd文件中的每一行,判断哪些用户是系统用户,哪些用户是普通用户并显示

[root@centos SC]#vim readline.sh
#!/bin/bash
while read passwdline
do
        uid=`echo $passwdline |cut -d: -f3`
        user=`echo $passwdline |cut -d: -f1`
        [ "$uid" -ge 1000 ] && echo "$user is common user" || echo "$user is system user"
done < /etc/passwd
unset passwdline user uid
~                                                                                            
~                                                                                            
~                                                                                            
~                                                                                            
~                                                                                            
~                                                                                            
~                                                                                            
"readline.sh" [New] 8L, 240C written                                       
[root@centos SC]#chmod +x readline.sh
[root@centos SC]#./readline.sh       
root is system user
bin is system user
daemon is system user
adm is system user
lp is system user
sync is system user
shutdown is system user
halt is system user
mail is system user
operator is system user
games is system user
ftp is system user
nobody is system user
systemd-bus-proxy is system user
systemd-network is system user
dbus is system user
polkitd is system user
abrt is system user
unbound is system user
tss is system user
libstoragemgmt is system user
rpc is system user
colord is system user
usbmuxd is system user
saslauth is system user
geoclue is system user
rtkit is system user
rpcuser is system user
nfsnobody is common user
radvd is system user
qemu is system user
ntp is system user
chrony is system user
setroubleshoot is system user
sssd is system user
pulse is system user
gdm is system user
gnome-initial-setup is system user
sshd is system user
avahi is system user
postfix is system user
tcpdump is system user
apache is system user
xmj is common user
huahua is common user

until

until

until CONDITION; do
          循环体
done

进入条件:CONDITION为false
退出条件:CONDITION为true

示例:

[root@centos SC]#vim h.sh
#!/bin/bash
i=1
######输出显示i=1-4当i=5时跳出脚本
until false;do
        [ $i -eq 5 ]&&break
        echo i=$i
        sleep 0.3
        let i++
done
~                                                                                     
~                                                                                     
~                                                                                     
~                                                                                     
~                                                                                     
~                                                                                     
~  
######测试结果                                                                                  
[root@centos SC]#./h.sh
i=1
i=2
i=3
i=4
[root@centos SC]#bash -x h.sh
+ i=1
+ false
+ '[' 1 -eq 5 ']'
+ echo i=1
i=1
+ sleep 0.3
+ let i++
+ false
+ '[' 2 -eq 5 ']'
+ echo i=2
i=2
+ sleep 0.3
+ let i++
+ false
+ '[' 3 -eq 5 ']'
+ echo i=3
i=3
+ sleep 0.3
+ let i++
+ false
+ '[' 4 -eq 5 ']'
+ echo i=4
i=4
+ sleep 0.3
+ let i++
+ false
+ '[' 5 -eq 5 ']'
+ break

创建无限循环

while true; do
       循环体
done

...

until false; do
       循环体
done

相关文章

  • for,while,until

    循环 循环执行 循环执行语句:for,while,until for for 执行机制:依次将列表中的元素赋值给“...

  • shell流程控制-until循环语句

    until语句until介绍until语法案例分享一、until介绍和while正好相反,until是条件为假开始...

  • Shell 循环语句(三) until 循环

    until 循环与 while 循环类似,也同样基于一个条件。但是 until 循环的判断条件正好与 while ...

  • while until 循环

    while [ 条件为真 ] do 循环体 done until [ 条件为假 ] do 循环体 ...

  • shell循环:while until

    循环次数不一定是固定的,更适合用while until 可以固定可以不固定 一,while语句结构 二,until...

  • Day5: 时间连词

    when, after, before, until, since, while, once, as, and a...

  • 流程控制 -- until循环

    until与while正好相反,while是条件成立时,才进入循环,而until是条件不成立时才进入循环,一旦复合...

  • 循环

    1、 times 2、for循环 3、普通的for语句 4 、while 5、until until条件和whi...

  • shell-10 until

    一、until介绍和while正好相反,until是条件为假开始执行,条件为真停止执行。 二、until语法 三、案例

  • until循环

    #until 循环(false时进入循环) 和while 循环相反i=1until (($i > 1))doech...

网友评论

      本文标题:for,while,until

      本文链接:https://www.haomeiwen.com/subject/sunzcxtx.html