Linux for循环之列表for循环

雨点打透心脏的1/2处 2022-06-12 04:56 903阅读 0赞

for循环是Linux shell 中最常用的结构。for 循环有三种结构:一种结构是列表for循环;第二种结构是不带列表for循环;第三种结构是类C风格的for循环。

本篇博文重点看列表for循环,列表for循环大的格式固定,在列表构成上分多种情景,如数字列表、字符串列表、命令列表、脚本传参列表等,下面一一来看。

列表for循环语句用于将一组命令执行已知的次数,语句基本格式如下

  1. for variable in (list)
  2. do
  3. command
  4. command
  5. ...
  6. done

其中,do 和 done之间的命令成为循环体,执行次数和list列表中常数或字符串的个数相同。当执行for循环时,首先将in 后 list 列表的第一个常数或字符串赋给循环变量,然后执行循环体;接着将list 列表中的第二个常数或字符串赋值给循环变量,再次执行循环体。这个过程将一直持续到list 列表中无其它常数或字符串,然后执行done命令后的命令序列。

ex1,列表for循环中list 列表为常数的情况

  1. #!/bin/bash
  2. #使用列表for循环显示5次欢迎操作
  3. for variable in 1 2 3 4 5
  4. do
  5. echo "Hello, welcome $variable times "
  6. done

这种示例的循环经常用于计数,范围被限定在1~5之间。如下是脚本执行结果,由于in 后面列表列出了5个参数,可以看出脚本执行5次欢迎操作。

  1. [zhangqi@localhost shellscript]$ sh for_ex1.sh
  2. Hello, welcome 1 times
  3. Hello, welcome 2 times
  4. Hello, welcome 3 times
  5. Hello, welcome 4 times
  6. Hello, welcome 5 times
  7. [zhangqi@localhost shellscript]$

Linux shell中支持列表for 循环中使用略写的计数方式,我们将脚本略作改进

ex2,列表为略写形式

  1. #!/bin/bash
  2. #使用列表for循环显示5次欢迎操作
  3. for variable in {1..5}
  4. do
  5. echo "Hello, welcome $variable times "
  6. done

执行后,结果同脚本1相同

  1. [zhangqi@localhost shellscript]$ sh for_ex2.sh
  2. Hello, welcome 1 times
  3. Hello, welcome 2 times
  4. Hello, welcome 3 times
  5. Hello, welcome 4 times
  6. Hello, welcome 5 times
  7. [zhangqi@localhost shellscript]$

上面示例种,我们将1~5进行略写,使其可以正常的与示例1输出相同的结果

ex3,列表为简写形式

  1. #!/bin/bash
  2. #使用列表for循环显示5次欢迎操作
  3. for variable in $(seq 1 5)
  4. do
  5. echo "Hello, welcome $variable times "
  6. done

seq 命令是Linux预设的外部命令,一般用于一堆数字的简化写法,可以参考linux常用命令之seq。

执行后,结果同上面相同,就不重复贴出来了。

ex4,按步数跳跃方式实现列表

  1. #!/bin/bash
  2. #使用列表for循环显示5次欢迎操作
  3. for variable in {1..5..2}
  4. do
  5. echo "Hello, welcome $variable times "
  6. done

in {1..5..2} 实现1~5之内的数字,按照步数2进行跳跃

运行下,看下结果

  1. [zhangqi@localhost shellscript]$ sh for_ex4.sh
  2. Hello, welcome 1 times
  3. Hello, welcome 3 times
  4. Hello, welcome 5 times
  5. [zhangqi@localhost shellscript]$

ex5、跳跃方式用seq表达

  1. [zhangqi@localhost shellscript]$ cat for_ex5.sh
  2. #!/bin/bash
  3. #使用列表for循环显示5次欢迎操作
  4. for variable in $(seq 1 2 5)
  5. do
  6. echo "Hello, welcome $variable times "
  7. done
  8. [zhangqi@localhost shellscript]$ sh for_ex5.sh
  9. Hello, welcome 1 times
  10. Hello, welcome 3 times
  11. Hello, welcome 5 times
  12. [zhangqi@localhost shellscript]$

ex6、用字符串表示列表

  1. [zhangqi@localhost shellscript]$ cat for_ex6.sh
  2. #!/bin/bash
  3. #使用列表for循环显示周一到周日对应的英文
  4. for day in Monday Tuesday Wednesday Thursday Friday Saturday Sunday
  5. do
  6. echo "$day"
  7. done
  8. [zhangqi@localhost shellscript]$ sh for_ex6.sh
  9. Monday
  10. Tuesday
  11. Wednesday
  12. Thursday
  13. Friday
  14. Saturday
  15. Sunday
  16. [zhangqi@localhost shellscript]$

ex7、使用命令表示列表

  1. [zhangqi@localhost shellscript]$ cat for_ex7.sh
  2. #!/bin/bash
  3. #使用命令打印数组
  4. for variable in `ls /`
  5. do
  6. echo "Every directory is $variable "
  7. done
  8. [zhangqi@localhost shellscript]$ sh for_ex7.sh
  9. Every directory is bin
  10. Every directory is boot
  11. Every directory is dev
  12. Every directory is etc
  13. Every directory is home
  14. Every directory is lib
  15. Every directory is lost+found
  16. Every directory is media
  17. Every directory is mnt
  18. Every directory is opt
  19. Every directory is proc
  20. Every directory is root
  21. Every directory is sbin
  22. Every directory is selinux
  23. Every directory is srv
  24. Every directory is sys
  25. Every directory is tmp
  26. Every directory is usr
  27. Every directory is var
  28. [zhangqi@localhost shellscript]$

这里的命令格式可以使用 $( command) 或 `command`,效果相同,这里就不再做展示了。

ex8、通过脚本传参实现里列表

  1. [zhangqi@localhost shellscript]$ cat for_ex8.sh
  2. #!/bin/bash
  3. echo "number of arguments is $#"
  4. echo "What you input is :"
  5. #使用命令打印数组
  6. for argument in "$*"
  7. do
  8. echo "$argument "
  9. done
  10. [zhangqi@localhost shellscript]$ sh for_ex8.sh 1 hello shell
  11. number of arguments is 3
  12. What you input is :
  13. 1 hello shell
  14. [zhangqi@localhost shellscript]$

可以看出,参数列表可以是数字,也可以是字符串,但是输入是以空格进行分隔的,如果存在空格,脚本执行时会认为存在另一个参数。

发表评论

表情:
评论列表 (有 0 条评论,903人围观)

还没有评论,来说两句吧...

相关阅读

    相关 for循环

    for循环 语法格式: > for (初始化语句;循环条件;初始化语句的改变)\{循环体\} 格式解释: > for:关键字 代表for循环 >

    相关 for循环

        循环:重复执行某一件或多件事   sun+=1 的意思是 sun=sum+1 break:跳出整个循环 continue:跳出本次循环,继续下次循环