Linux学习基础之Shell编程——流程控制——for循环、while循环和until循环

for 循环的两种语法,语法一适用于不知道循环次数时,

                                     语法二适用于知道循环次数时;

1、语法一:

for   变量  in  值1  值2  值3...

     do 

           程序

     done

注:如上,有几个值,就会执行几次;

[root@localhost sh]# vim forTest1.sh
#!/bin/bash

#打印时间

for time in morning noon afternoon evening

    do

       echo "This time is $time ! "

    done


~                                                                                                 
~                      
[root@localhost sh]# chmod 755 forTest1.sh 
[root@localhost sh]# ./forTest1.sh 
This time is morning ! 
This time is noon ! 
This time is afternoon ! 
This time is evening ! 
[root@localhost sh]# 

 

示例2:批量解压缩脚本

#!/bin/bash
#批量解压缩脚本

cd /lamp

ls *.tar.gz >ls.log

for i in $(cat ls.log)

    do

      tar -zxf $i &>/dev/null

    done
rm -rf /lamp/ls.log

~                                                                                                 
~          

如上,in 后面的内容需要具备:无论以什么格式,一定要有个“分隔”的内容,

比如如上 ls *.tar.gz >ls.log 执行结果是,每一个文件名在ls.log中会占据一行,

也就是说,这个文件中的分隔符是  换行符;

示例三:

[root@localhost sh]# pwd
/root/sh
[root@localhost sh]# vim forTest3.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@localhost sh]# chmod 755 forTest3.sh 
[root@localhost sh]# ./forTest3.sh 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@localhost sh]# ls
beifenmysql.sh  caseTest1.sh    forTest1.sh  inputwhat.sh         orld          yudingyibianliang
canshu1.sh      caseTest2.sh    forTest2.sh  jiafajisuanqi.sh     shili3.sh     zhengzetest
canshu4.sh      ceshiifelse.sh  forTest3.sh  judgeapachestart.sh  student1.txt
canshu6.sh      fenquused.sh    hello.sh     ls.log               student.txt
[root@localhost sh]# ll
总用量 88
-rwxr-xr-x. 1 root root 278 12月 23 23:00 beifenmysql.sh
-rwxr-xr-x. 1 root root  45 12月 16 17:13 canshu1.sh
-rwxr-xr-x. 1 root root 327 12月 16 17:49 canshu4.sh
-rwxrw-r-x. 1 root root 549 12月 16 19:09 canshu6.sh
-rwxr-xr-x. 1 root root 327 12月 30 23:04 caseTest1.sh
-rwxr-xr-x. 1 root root 536 12月 30 23:24 caseTest2.sh
-rwxr-xr-x. 1 root root 580 12月 23 23:29 ceshiifelse.sh
-rwxr-xr-x. 1 root root 269 12月 17 14:11 fenquused.sh
-rwxr-xr-x. 1 root root 129 12月 30 23:56 forTest1.sh
-rw-r--r--. 1 root root 161 12月 31 00:23 forTest2.sh
-rwxr-xr-x. 1 root root 133 12月 31 00:27 forTest3.sh
-rwxr-xr-x. 1 root root  97 12月 16 09:24 hello.sh
-rwxr-xr-x. 1 root root 521 12月 24 00:22 inputwhat.sh
-rwxr-xr-x. 1 root root 114 12月 16 17:26 jiafajisuanqi.sh
-rwxr-xr-x. 1 root root 428 12月 23 23:38 judgeapachestart.sh
-rw-r--r--. 1 root root 207 12月 31 00:28 ls.log
-rw-r--r--. 1 root root  97 12月 17 08:41 orld 
-rwxr-xr-x. 1 root root 204 12月 16 17:36 shili3.sh
-rw-r--r--. 2 root root  97 12月 17 08:52 student1.txt
-rw-r--r--. 1 root root  55 12月 17 05:49 student.txt
-rwxr-xr-x. 1 root root 428 12月 16 18:50 yudingyibianliang
-rw-r--r--. 1 root root 249 12月 17 02:24 zhengzetest
[root@localhost sh]# 

2、语法二:

for((初始值;循环控制条件;变量变化))

     do

            程序

     done

示例1、

[root@localhost sh]# vim forTest4.sh
#!/bin/bash

#从1加到100

s=0

for((i=1;i<=100;i=i+1))

    do

        s=$(($s+$i))

    done
echo "The sum of 1+2+3+...+100 is :$s"

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

示例2、

[root@localhost sh]# vim forTest5.sh

#!/bin/bash

#批量天剑指定数量的用户

read -p "Please imput user name:" -t 30 name
read -p "Please imput the number of users:" -t 30 num
read -p "Please imput the password of users:" -t 30 pass

if [ ! -z "$name" -a ! -z "$num" -a ! -z "$pass" ]
    then

      y=$(echo $num | sed 's/^[0-9]*//g')

         if [ -z "$y" ]

            then
              for ((i=1;i<$num;i=i+1))

                 do
                   useradd $name$i &>/dev/null
                     echo $pass | passwd --stdin $name$i &>/dev/null

                 done
          fi
fi
~                                                                                                 
~                                                                                                 
                 [root@localhost sh]# chmod 755 forTest5.sh 
[root@localhost sh]# ./forTest5.sh 
[root@localhost sh]# ./forTest5.sh 
Please imput user name:root
Please imput the number of users:12
Please imput the password of users:123456
[root@localhost sh]# vim forTest5.sh
[root@localhost sh]# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
rtkit:x:499:499:RealtimeKit:/proc:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
haldaemon:x:68:68:HAL daemon:/:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
apache:x:48:48:Apache:/var/www:/sbin/nologin
saslauth:x:498:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
pulse:x:497:496:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
zhouxueli:x:500:500:zhouxueli:/home/zhouxueli:/bin/bash
sc:x:501:501::/home/sc:/bin/bash
bimm:x:502:503::/home/bimm:/bin/bash
cangls:x:503:504::/home/cangls:/bin/bash
st:x:504:506::/home/st:/bin/bash
lamp:x:505:508::/home/lamp:/bin/bash
root1:x:506:509::/home/root1:/bin/bash
root2:x:507:510::/home/root2:/bin/bash
root3:x:508:511::/home/root3:/bin/bash
root4:x:509:512::/home/root4:/bin/bash
root5:x:510:513::/home/root5:/bin/bash
root6:x:511:514::/home/root6:/bin/bash
root7:x:512:515::/home/root7:/bin/bash
root8:x:513:516::/home/root8:/bin/bash
root9:x:514:517::/home/root9:/bin/bash
root10:x:515:518::/home/root10:/bin/bash
root11:x:516:519::/home/root11:/bin/bash
[root@localhost sh]# 
    

注意上方在写时:

1) if   [  条件语句  ]  : if 和[  ]之间有空格,[  ]  和中间的内容之间,前后有空格,特别注意

2)上方符号比较多,特别注意;

 

while循环

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

2、格式:

 while  [  条件判断式  ]                          #注意条件判断式前后的空格

     do  

           程序

     done

示例1:计算1+2+3+。。。+100的值

[root@localhost sh]# vim whileTest1.sh
#!/bin/bash

#从1 加到100

i=1

s=0

while [ $i -le 100 ]
# i小于等于100

#如果变量i的值小于等于100,则执行循环

    do
         s=$(($s +$i))

         i=$(($i + 1))

    done
echo "The sum is:$s"

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

二、until循环

1、until循环,和while循环相反,until循环式只要条件判断式不成立则进行循环,

并执行循环程序。一旦循环条件成立,则终止循环

2、格式:

until   [  条件判断式  ]

    do 

         程序

     done

示例2:

[root@localhost sh]# cp whileTest1.sh untilTest.sh
[root@localhost sh]# ls
beifenmysql.sh  caseTest2.sh    forTest3.sh   jiafajisuanqi.sh     student1.txt       zhengzetest
canshu1.sh      ceshiifelse.sh  forTest4.sh   judgeapachestart.sh  student.txt
canshu4.sh      fenquused.sh    forTest5.sh   ls.log               untilTest.sh
canshu6.sh      forTest1.sh     hello.sh      orld                 whileTest1.sh
caseTest1.sh    forTest2.sh     inputwhat.sh  shili3.sh            yudingyibianliang
[root@localhost sh]# vim untilTest.sh 

#!/bin/bash

#从1 加到100

i=1

s=0

until [ $i -ge 100 ]

#如果变量i的值小于等于100,则执行循环

    do
         s=$(($s +$i))

         i=$(($i + 1))

    done
echo "The sum is:$s"

~              

[root@localhost sh]# chmod 755 untilTest.sh 
[root@localhost sh]# ./untilTest.sh 
The sum is:4950
[root@localhost sh]# vim untilTest.sh 

#!/bin/bash

#从1 加到100

i=1

s=0

until [ $i -gt 100 ]

#如果变量i的值小于等于100,则执行循环

    do
         s=$(($s +$i))
            
         i=$(($i + 1))
            
    done   
echo "The sum is:$s"
[root@localhost sh]# ./untilTest.sh 
The sum is:5050
    

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值