设为首页 收藏本站
查看: 644|回复: 0

shell编程中for/while循环命令

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-1-27 08:29:11 | 显示全部楼层 |阅读模式
一、for命令
    在shell编程中,有时我们需要重复执行一直命令直至达到某个特定的条件,bash shell中,提供了for命令,允许你创建一个遍历一系列值的循环,每次迭代都通过一个该系列中的值执行一组预定义的命令。

for的基本格式:
    for var in list
    do
        commands

    done
    在list中,你提供了迭代中要用的一系列值。在每个迭代中,变量var包含列表中的当前值,第一个迭代会适用列表中的第一个值,第二个迭代使用第二个值,以此类推,直至列表中的所有值都过一遍。

1.1读取列表中的值
1
2
3
4
5
6
7
8
9
10
11
12
[iyunv@sh shell]# cat for1.sh
#!/bin/bash
for test in aaa bbb ccc ddd
do
        echo the next state is $test
done

[iyunv@sh shell]# sh for1.sh
the next state is aaa
the next state is bbb
the next state is ccc
the next state is ddd



$test变量的值会在shell脚本的最后一次迭代中一直保持有效,除非你修改了它
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[iyunv@sh shell]# cat for1.sh
#!/bin/bash
for test in aaa bbb ccc ddd
do
    echo the next state is $test
done
echo "the last state we visited was $test"
test=fff
echo "wait. now we're visiting $test"


[iyunv@sh shell]# sh for1.sh
the next state is aaa
the next state is bbb
the next state is ccc
the next state is ddd
the last state we visited was ddd
wait. now we're visiting fff




1.2读取列表中的复杂值
在shell脚本中,优势你会遇到难处理的数。下面是个给shell脚本程序员带来麻烦的经典例子:
1
2
3
4
5
6
7
8
9
10
11
[iyunv@sh shell]# cat for2.sh
#!/bin/bash
for test in I don't know if this'll work
do
    echo "word:$test"
done

[iyunv@sh shell]# sh for2.sh
word:I
word:dont know if thisll
word:work



解决办法:使用转义符或者双引号
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[iyunv@sh shell]# cat for2.sh
#!/bin/bash
for test in I don\'t know if "this'll" work
do
    echo "word:$test"
done

[iyunv@sh shell]# sh for2.sh
word:I
word:don't
word:know
word:if
word:this'll
word:work




记住:for循环假定每一个值都是用空格分割的,如果在单独的数字值中有空格,那么你必须使用双引号来将这些值圈起来。


1.3从变量读取列表
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[iyunv@sh shell]# cat for3.sh
############################
#!/bin/bash

list="aaa bbb ccc ddd eee"
list=$list" Connecticut"
for state in $list

do
  echo "Have you ever visited $state"
done


[iyunv@sh shell]# sh for3.sh
Have you ever visited aaa
Have you ever visited bbb
Have you ever visited ccc
Have you ever visited ddd
Have you ever visited eee
Have you ever visited Connecticut




1.4从命令读取值
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[iyunv@sh shell]# cat for4.sh
#!/bin/bash
file="/root/shell/states"  #如果是在当前不用绝对路径,file=“states”即可

for state in `cat $file`
do
  echo "Visit beautiful $state"
done

[iyunv@sh shell]# sh for4.sh
Visit beautiful shanghai
Visit beautiful beijing
Visit beautiful hangzhou
Visit beautiful nanjing
Visit beautiful guangzhou

[iyunv@sh shell]# cat states
shanghai
beijing
hangzhou
nanjing
guangzhou



1.5更改字段分隔符
  • 空格;
  • 制表符:
  • 换行符


    如果bash shell在数据中看到了这些字符中任意一个,它就会假定你在列表中开始了一个新的数据段。
    要解决这个问题,你可以在你shell脚本中临时更改IFS环境变量的值来限制一下被bash shell当作字段分隔符的字符。但这种方式有点奇怪,比如,如果你行IFS的值使其只能识别换行符,你必须这么做:

    IFS=$'\n'

    将这个语句加入到脚本中,告诉bash shell在数据值中忽略空格和制表符。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[iyunv@sh shell]# cat for5.sh
#!/bin/bash
file="states"

IFS=$'\n'
for state in `cat $file`
do
  echo "Visit beautiful $state"
done

[iyunv@sh shell]# sh for5.sh
Visit beautiful shanghai
Visit beautiful beijing
Visit beautiful hangzhou
Visit beautiful nanjing
Visit beautiful guang zhou
Visit beautiful nan ning
Visit beautiful jiang nan



在处理长脚本中,可能在一个地方需要修改IFS的值,然后忘掉它在脚本中其他地方还原默认值。
    例如:

        IFS.OLD=$IFS

        IFS=$'\n'

        <use the new IFS value in code>

        IFS=$IFS.OLD


其他的IFS值,如:在/etc/passwd中可能用到
    IFS=:

也可以赋值多个IFS:
    IFS=$'\n:;"'


1.6用通配符读取目录
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[iyunv@sh shell]# cat for6.sh
#!/bin/bash
for file in /home/*
do

  if [ -d "$file" ];then
    echo "$file is a directory"
  elif [ -f "$file" ];then
    echo "$file is a file"
  fi

done

[iyunv@sh shell]# sh for6.sh
/home/apache-tomcat-8.0.28.tar.gz is a file
/home/dir1 is a directory
/home/dir2 is a directory
/home/fie1 is a file
/home/fie2 is a file
/home/fie22 is a file



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[iyunv@sh shell]# cat for7.sh
#!/bin/bash
for file in /home/* /home/badtest
do

  if [ -d "$file" ];then
    echo "$file is a directory"
  elif [ -f "$file" ];then
    echo "$file is a file"
  else
    echo "$file doesn't exist"
  fi

done
[iyunv@sh shell]# sh for7.sh
/home/apache-tomcat-8.0.28.tar.gz is a file
/home/dir1 is a directory
/home/dir2 is a directory
/home/fie1 is a file
/home/fie2 is a file
/home/fie22 is a file
/home/badtest doesn't exist




二、C语言风格的for命令
2.1 C语言风格的for命令
    C语言的for命令有一个用来指明变量的特殊方法、一个必须保持成立才能继续迭代的条件,以及另一个为每个迭代改变变量的方法。当指定的条件不成立时,for循环就会停止。条件等式通过标准的数字符号定义。
    for (i=0; i<10; i++)

    {
        printf("The next number is %d\n",i):

    }

    第一部分将一个默认值赋给该变量,中间的部分定义了循环重复的条件,当定义的条件不成立时,for循环就停止迭代,最后一部分定义了迭代的过程。


bash中C语言风格的for循环的基本格式:
    for (( variable assignment;condition;iteration process))

    for (( a = 1; a < 10; a++ ))


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[iyunv@sh shell]# cat c1.sh
#!/bin/bash

for (( i=1; i < 10; i++ ))
do
  echo "The next number is $i"
done

[iyunv@sh shell]# sh c1.sh
The next number is 1
The next number is 2
The next number is 3
The next number is 4
The next number is 5
The next number is 6
The next number is 7
The next number is 8
The next number is 9




2.2使用多个变量

    C语言风格的for命令也允许你为迭代使用多个变量。循环会单独处理每个变量,允许你为每个变量定义不同的迭代过程。当你有多个变量时,你只能在for循环中定义一种条件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[iyunv@sh shell]# cat c2.sh
#!/bin/bash

for (( a=1, b=10; a <= 10; a++, b-- ))
do
  echo "$a - $b"
done

[iyunv@sh shell]# sh c2.sh
1 - 10
2 - 9
3 - 8
4 - 7
5 - 6
6 - 5
7 - 4
8 - 3
9 - 2
10 - 1



三、while命令
    while命令在某种意义上是if-then语句和for循环的混杂体。while命令允许你定义一个要测试的命令,然后循环执行一组命令,只要定义的测试命令返回的是退出状态码0,它就会在每个迭代的一开始测试test命令,在测试test命令返回非零退出状态码是,while命令会停止执行那组命令。

3.1while的基本格式
    while test command

    do

      other command

    done


    while命令指定的test命令的退出状态码必须随着循环中运行的命令改变,如果退出状态码从不改变,那么while循环将会一直不停地循环。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[iyunv@sh shell]# cat w1.sh
#!/bin/bash

var1=10
while [ $var1 -gt 0 ]
do
  echo $var1
  var1=$[ $var1 - 1 ]
done

[iyunv@sh shell]# sh w1.sh
10
9
8
7
6
5
4
3
2
1



    在这些命令中,测试条件中用到的变量必须被修改,否则你就进入了一个无限循环。


3.2使用多个测试命令
    while命令允许你在while语句行定义多个测试命令,只有最后一个测试命令的退出状态码会被用来决定什么时候介绍循环。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
[iyunv@sh shell]# cat w2.sh

#!/bin/bash

var1=10

while echo $var1
    [ $var1 -ge 0 ]
do
  echo "This is inside the loop"
  var1=$[ $var1 - 1 ]
done

[iyunv@sh shell]# sh w2.sh
10
This is inside the loop
9
This is inside the loop
8
This is inside the loop
7
This is inside the loop
6
This is inside the loop
5
This is inside the loop
4
This is inside the loop
3
This is inside the loop
2
This is inside the loop
1
This is inside the loop
0
This is inside the loop
-1



    在上面的例子中,while语句中定义了两个测试命令:

        while echo $var1

            [ $var1 -ge 0 ]

在while语句中,在每次迭代中所有的测试命令都会被执行,包括最后一个命令不成立的最后那次循环。注意每个测试命令都是在单独的一行上。




运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-169976-1-1.html 上篇帖子: shell脚本案例 下篇帖子: Powershell 批量移动计算机到指定ou
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表