1for命令

for命令的基本格式:

  1. for var in list
  2. do
  3. commands
  4. done

在list参数中,你需要提供迭代中要用到的一系列值。

1.1读取列表中的值

例子:

  1. $ vim test1
  2. #!/bin/bash
  3. # testing the for variable after the looping
  4. for test in Alabama Alaska Arizona Arkansas California Colorado
  5. do
  6. echo "The next state is $test"
  7. done
  8. echo "The last state we visited was $test"
  9. test=Connecticut
  10. echo "Wait, now we're visiting $test"
  11.  
  12. 执行结果:
  13. $ ./test1
  14. The next state is Alabama
  15. The next state is Alaska
  16. The next state is Arizona
  17. The next state is Arkansas
  18. The next state is California
  19. The next state is Colorado
  20. The last state we visited was Colorado
  21. Wait, now we're visiting Connecticut

1.2读取列表中的复杂值

看这个例子:

  1. $ vim test2
  2. #!/bin/bash
  3. # another example of how not to use the for command
  4. for test in I don\'t know if "this'll" work
  5. do
  6. echo "word:$test"
  7. done
  8.  
  9. 结果:
  10. $ ./test2
  11. word:I
  12. word:don't
  13. word:know
  14. word:if
  15. word:this'll
  16. word:work

for循环每个值都是用空格分割的。如果有包含空格的数据值,也会导致这种类似的问题,所以处理方法可以采用上面的第二种(加引号)的方式。

1.3从变量读取列表

看例子:

  1. $ vim test4
  2. #!/bin/bash
  3. # using a variable to hold the list
  4. list="Alabama Alaska Arizona Arkansas Colorado"
  5. list=$list" Connecticut"
  6. for state in $list
  7. do
  8. echo "Have you ever visited $state?"
  9. done
  10.  
  11. 结果:
  12. $ ./test4
  13. Have you ever visited Alabama?
  14. Have you ever visited Alaska?
  15. Have you ever visited Arizona?
  16. Have you ever visited Arkansas?
  17. Have you ever visited Colorado?
  18. Have you ever visited Connecticut?

1.4从命令读取值

  1. $ vim test5
  2. #!/bin/bash
  3. # reading values from a file
  4. file="states"
  5. for state in $(cat $file)
  6. do
  7. echo "Visit beautiful $state"
  8. done
  9.  
  10. $ cat states
  11. Alabama
  12. Alaska
  13. Arizona
  14. Arkansas
  15. Colorado
  16. Connecticut
  17. Delaware
  18. Florida
  19. Georgia
  20. 执行结果:
  21. $ ./test5
  22. Visit beautiful Alabama
  23. Visit beautiful Alaska
  24. Visit beautiful Arizona
  25. Visit beautiful Arkansas
  26. Visit beautiful Colorado
  27. Visit beautiful Connecticut
  28. Visit beautiful Delaware
  29. Visit beautiful Florida
  30. Visit beautiful Georgia

1.5更改字段分隔符

IFS叫作内部字段分隔符

IFS环境变量定义了bash shell用作字段分隔符的一系列字符。默认情况下,bash shell会将下列字符当作字段分隔符:

  • 空格
  • 制表符
  • 换行符

如果bash shell在数据中看到了这些字符中的任意一个,它就会假定这表明了列表中一个新数据字段的开始。

但是我们可以在shell脚本中临时更改IFS环境变量的值来限制被bash shell当作字段分隔符的字符。例如,如果你想修改IFS的值,使其只能识别换行符,那就必须这么做:

  1. IFS=$'\n'

将这个语句加入到脚本中,告诉bash shell在数据值中忽略空格和制表符。对前一个脚本使用这种方法,将获得如下输出。

  1. $ vim test5b
  2. #!/bin/bash
  3. # reading values from a file
  4. file="states"
  5. IFS=$'\n'
  6. for state in $(cat $file)
  7. do
  8. echo "Visit beautiful $state"
  9. done
  10.  
  11. $ ./test5b
  12. Visit beautiful Alabama
  13. Visit beautiful Alaska
  14. Visit beautiful Arizona
  15. Visit beautiful Arkansas
  16. Visit beautiful Colorado
  17. Visit beautiful Connecticut
  18. Visit beautiful Delaware
  19. Visit beautiful Florida
  20. Visit beautiful Georgia
  21. Visit beautiful New York
  22. Visit beautiful New Hampshire
  23. Visit beautiful North Carolina

1.6用通配符读取目录

  1. $ vim test6
  2. #!/bin/bash
  3. # iterate through all the files in a directory
  4. for file in /home/rich/test/*
  5. do
  6. if [ -d "$file" ]
  7. then
  8. echo "$file is a directory"
  9. elif [ -f "$file" ]
  10. then
  11. echo "$file is a file"
  12. fi
  13. done
  14.  
  15. $ ./test6
  16. /home/rich/test/dir1 is a directory
  17. /home/rich/test/myprog.c is a file
  18. /home/rich/test/myprog is a file
  19. /home/rich/test/myscript is a file
  20. /home/rich/test/newdir is a directory
  21. /home/rich/test/newfile is a file
  22. /home/rich/test/newfile2 is a file
  23. /home/rich/test/testdir is a directory
  24. /home/rich/test/testing is a file
  25. /home/rich/test/testprog is a file
  26. /home/rich/test/testprog.c is a file

for命令会遍历/home/rich/test/*输出的结果。该代码用test命令测试了每个条目(使用方括号方法),以查看它是目录还是文件。for后面可以接多个需要循环遍历的路径。注意这里的判断条件“$file”用引号包围起来了,这是为了避免我们目录名和文件名有空格而导致误认为有多个文件而出错。

2 C语言风格的for命令

例一:使用单一变量

  1. $ vim test8
  2. #!/bin/bash
  3. # testing the C-style for loop
  4. for (( i=1; i <= 10; i++ ))
  5. do
  6. echo "The next number is $i"
  7. done
  8.  
  9. $ ./test8
  10. The next number is 1
  11. The next number is 2
  12. The next number is 3
  13. The next number is 4
  14. The next number is 5
  15. The next number is 6
  16. The next number is 7
  17. The next number is 8
  18. The next number is 9
  19. The next number is 10

例二:使用多个变量

  1. $ vim test9
  2. #!/bin/bash
  3. # multiple variables
  4. for (( a=1, b=10; a <= 10; a++, b-- ))
  5. do
  6. echo "$a - $b"
  7. done
  8.  
  9. $ ./test9
  10. 1 - 10
  11. 2 - 9
  12. 3 - 8
  13. 4 - 7
  14. 5 - 6
  15. 6 - 5
  16. 7 - 4
  17. 8 - 3
  18. 9 - 2
  19. 10 - 1

3while命令

while命令的格式是:

  1. while test command
  2. do
  3. other commands
  4. done

while命令的关键在于所指定的test command的退出状态码必须随着循环中运行的命令而改变。如果退出状态码不发生变化, while循环就将一直不停地进行下去。

  1. $ vim test11
  2. #!/bin/bash
  3. # testing a multicommand while loop
  4. var1=5
  5. while echo $var1
  6. [ $var1 -ge 0 ]
  7. do
  8. echo "This is inside the loop"
  9. var1=$[ $var1 - 1 ]
  10. done
  11.  
  12. $ ./test11
  13. 5
  14. This is inside the loop
  15. 4
  16. This is inside the loop
  17. 3
  18. This is inside the loop
  19. 2
  20. This is inside the loop
  21. 1
  22. This is inside the loop
  23. 0
  24. This is inside the loop
  25. -1

while循环会在var1变量等于0时执行echo语句,然后将var1变量的值减一。接下来再次执行测试命令,用于下一次迭代。echo测试命令被执行并显示了var变量的值(现在小于0了)。直到shell执行test测试命令,whle循环才会停止。

4until命令

until命令和while命令工作的方式完全相反。until命令要求你指定一个通常返回非零退出状态码的测试命令。只有测试命令的退出状态码不为0,bash shell才会执行循环中列出的命令。一旦测试命令返回了退出状态码0,循环就结束了。

格式:

  1. until test commands
  2. do
  3. other commands
  4. done

下面是使用until命令的一个例子。

  1. $ vim test12
  2. #!/bin/bash
  3. # using the until command
  4. var1=100
  5. until [ $var1 -eq 0 ]
  6. do
  7. echo $var1
  8. var1=$[ $var1 - 25 ]
  9. done
  10.  
  11. $ ./test12
  12. 100
  13. 75
  14. 50
  15. 25

使用多个测试命令与while一样。

5嵌套循环

在for循环中嵌套for循环的简单例子:

  1. $ vim test14
  2. #!/bin/bash
  3. # nesting for loops
  4. for (( a = 1; a <= 3; a++ ))
  5. do
  6. echo "Starting loop $a:"
  7. for (( b = 1; b <= 3; b++ ))
  8. do
  9. echo " Inside loop: $b"
  10. done
  11. done
  12.  
  13. $ ./test14
  14. Starting loop 1:
  15. Inside loop: 1
  16. Inside loop: 2
  17. Inside loop: 3
  18. Starting loop 2:
  19. Inside loop: 1
  20. Inside loop: 2
  21. Inside loop: 3
  22. Starting loop 3:
  23. Inside loop: 1
  24. Inside loop: 2
  25. Inside loop: 3

在while循环内部放置一个for循环:

  1. $ vim test15
  2. #!/bin/bash
  3. # placing a for loop inside a while loop
  4. var1=5
  5. while [ $var1 -ge 0 ]
  6. do
  7. echo "Outer loop: $var1"
  8. for (( var2 = 1; $var2 < 3; var2++ ))
  9. do
  10. var3=$[ $var1 * $var2 ]
  11. echo " Inner loop: $var1 * $var2 = $var3"
  12. done
  13. var1=$[ $var1 - 1 ]
  14. done
  15.  
  16. $ ./test15
  17. Outer loop: 5
  18. Inner loop: 5 * 1 = 5
  19. Inner loop: 5 * 2 = 10
  20. Outer loop: 4
  21. Inner loop: 4 * 1 = 4
  22. Inner loop: 4 * 2 = 8
  23. Outer loop: 3
  24. Inner loop: 3 * 1 = 3
  25. Inner loop: 3 * 2 = 6
  26. Outer loop: 2
  27. Inner loop: 2 * 1 = 2
  28. Inner loop: 2 * 2 = 4
  29. Outer loop: 1
  30. Inner loop: 1 * 1 = 1
  31. Inner loop: 1 * 2 = 2
  32. Outer loop: 0
  33. Inner loop: 0 * 1 = 0
  34. Inner loop: 0 * 2 = 0

混用until和while循环:

  1. $ vim test16
  2. #!/bin/bash
  3. # using until and while loops
  4. var1=3
  5. until [ $var1 -eq 0 ]
  6. do
  7. echo "Outer loop: $var1"
  8. var2=1
  9. while [ $var2 -lt 5 ]
  10. do
  11. var3=$(echo "scale=4; $var1 / $var2" | bc)
  12. echo " Inner loop: $var1 / $var2 = $var3"
  13. var2=$[ $var2 + 1 ]
  14. done
  15. var1=$[ $var1 - 1 ]
  16. done
  17.  
  18. $ ./test16
  19. Outer loop: 3
  20. Inner loop: 3 / 1 = 3.0000
  21. Inner loop: 3 / 2 = 1.5000
  22. Inner loop: 3 / 3 = 1.0000
  23. Inner loop: 3 / 4 = .7500
  24. Outer loop: 2
  25. Inner loop: 2 / 1 = 2.0000
  26. Inner loop: 2 / 2 = 1.0000
  27. Inner loop: 2 / 3 = .6666
  28. Inner loop: 2 / 4 = .5000
  29. Outer loop: 1
  30. Inner loop: 1 / 1 = 1.0000
  31. Inner loop: 1 / 2 = .5000
  32. Inner loop: 1 / 3 = .3333
  33. Inner loop: 1 / 4 = .2500

6控制循环

6.1break命令

1. 跳出单个循环

用for循环举例,当然break命令同样适用于while和until循环:

  1. $ vim test17
  2. #!/bin/bash
  3. # breaking out of a for loop
  4. for var1 in 1 2 3 4 5 6 7 8 9 10
  5. do
  6. if [ $var1 -eq 5 ]
  7. then
  8. break
  9. fi
  10. echo "Iteration number: $var1"
  11. done
  12. echo "The for loop is completed"
  13.  
  14. $ ./test17
  15. Iteration number: 1
  16. Iteration number: 2
  17. Iteration number: 3
  18. Iteration number: 4
  19. The for loop is completed

2. 跳出外部循环

有时你在内部循环,但需要停止外部循环。break命令接受单个命令行参数值:

  1. break n

6.2continue命令

continue命令可以提前中止某次循环中的命令,但并不会完全终止整个循环。可以在循环内部设置shell不执行命令的条件。

一个在for循环中使用continue命令的简单例子:

  1. $ vim test21
  2. #!/bin/bash
  3. # using the continue command
  4. for (( var1 = 1; var1 < 9; var1++ ))
  5. do
  6. if [ $var1 -gt 3 ] && [ $var1 -lt 6 ]
  7. then
  8. continue
  9. fi
  10. echo "Iteration number: $var1"
  11. done
  12.  
  13. $ ./test21
  14. Iteration number: 1
  15. Iteration number: 2
  16. Iteration number: 3
  17. Iteration number: 6
  18. Iteration number: 7
  19. Iteration number: 8

同样地,continue命令也有像break一样的另外两中用法,我们来看一下

  1. continue n

Shell编程—结构化命令(2)的更多相关文章

  1. Shell编程—结构化命令

    1使用if-then语句 f-then语句有如下格式. if command then commands fi bash shell的if语句会运行if后面的那个命令.如果该命令的退出状态码是0(该命 ...

  2. Linux编程 23 shell编程(结构化条件判断 命令if -then , if-then ... elif-then ...else,if test)

    一.概述 在上一篇里讲到了shell脚本,shell按照命令在脚本中出现的顺序依次进行处理,对于顺序操作已经足够了,但许多程序要求对shell脚本中的命令加入一些逻辑流程控制,这样的命令通常叫做 结构 ...

  3. Linux编程 24 shell编程(结构化 if [ condition ] 数值比较,字符串比较)

    一.概述 接着上篇讲的结构化命令,最后讲到了test命令的另一种写法 if [ condition ],它的语法格式如下: --格式如下: if [ condition ] then commands ...

  4. shell的结构化命令

    shell在逻辑流程控制这里会根据设置的变量值的条件或其他命令的结果跳过一些命令或者循环执行的这些命令.这些命令通常称为结构化命令 1.if-then语句介绍 基本格式 if command then ...

  5. Linux&shell之结构化命令进阶

    写在前面:案例.常用.归类.解释说明.(By Jim) for命令重复一系列的命令是一种常见的编程实践. #!/bin/bash # basic for command for test in A B ...

  6. Linux&shell之结构化命令

    写在前面:案例.常用.归类.解释说明.(By Jim)使用if-then语句如果命令的退出状态是0(成功执行命令),将执行then后面的所有命令.如果命令的退出状态是0以外的其他值,那么then后面的 ...

  7. shell初级-----结构化命令

    if-then语句 bash shell的if语句会执行if后面的那个命令,如果该命令的退出码状态为0会执行then部分的命令,如果是其他值不会执行. 格式如下: if command then co ...

  8. 《Linux命令行与shell脚本编程大全》第十二章 使用结构化命令

    许多程序要就对shell脚本中的命令施加一些逻辑控制流程. 结构化命令允许你改变程序执行的顺序.不一定是依次进行的 12.1 使用if-then语句 如下格式: if command then     ...

  9. Shell 语法之结构化命令(流程控制)

    许多程序在脚本命令之间需要某种逻辑流控制,允许脚本根据变量值的条件或者其他命令的结果路过一些命令或者循环执行这些命令.这些命令通常被称为结构化命令.和其他高级程序设计语言一样,shell提供了用来控制 ...

随机推荐

  1. 对于Javaweb初学者的一些坑。#Javaweb

    1.在配置好Tomcat之后 ,编译阶段发现报错 这种对于我个人来说一般有两种情况: ①在编写代码时(比如servlet)发现爆红,一般是maven的依赖没有导入,这个时候在xml文件中导入需要的包的 ...

  2. sscanf,sprintf(思修课的收获)

    转载的,就是做个笔记 sprintf函数原型为 int sprintf(char *str, const char *format, ...).作用是格式化字符串,具体功能如下所示: (1)将数字变量 ...

  3. PHP imagealphablending - 设定图像的混色模式

    imagealphablending — 设定图像的混色模式.高佣联盟 www.cgewang.com 语法 bool imagealphablending ( resource $image , b ...

  4. Docker这些none:none的镜像,难道就不配拥有名字吗

    1 前言 欢迎访问南瓜慢说 www.pkslow.com获取更多精彩文章! 搞容器开发一段时间后,想看看都有哪些镜像,执行了一下docker images -a,蒙圈了,有一堆<none> ...

  5. 关于json 是字符串还是对象的问题

    是用ajax提交的时候,json应该是字符串形式: 响应的内容,根据设置处理不同,可能是对象形式:也可能是字符串形式. 如果是字符串形式,可转化成对象形式 再进行处理. 以下常用的几个转换函数:看名字 ...

  6. NCoreCoder.Aop 国庆更新

    原本的IAopActors如下 public interface IAopActors { object Execute(AopContext context); Task<TResult> ...

  7. SPP、ASPP、RFB、CBAM

    SPP:ASPP:将pooling 改为了 空洞卷积RFB:不同大小的卷积核和空洞卷积进行组合,认为大的卷积应该有更大的感受野. CBAM:空间和通道的注意力机制 SPP: Spatial Pyram ...

  8. Docker容器网络-实现篇

    通常,Linux容器的网络是被隔离在它自己的Network Namespace中,其中就包括:网卡(Network Interface).回环设备(Loopback Device).路由表(Routi ...

  9. 简单Web服务器

    import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.F ...

  10. find the lowest number location

    before #设定路径列表Path def find_path2(heightmap, x, y, water_level=557,path=[]): #global path #设定坐标 右0 左 ...