Linux for循环之列表for循环详解
作者:姜小白-
文章详细介绍了Linux shell中的for循环结构,重点分析列表for循环的三种形式:数字列表、字符串列表、命令列表及脚本传参列表,通过使用seq命令和跳步方式实现循环控制,并说明字符串列表的使用方法
for循环是Linux shell 中最常用的结构。
for 循环有三种结构:
- 一种结构是列表for循环
- 第二种结构是不带列表for循环
- 第三种结构是类C风格的for循环
本篇博文重点看列表for循环,列表for循环大的格式固定,在列表构成上分多种情景,如数字列表、字符串列表、命令列表、脚本传参列表等,下面一一来看。
列表for循环语句用于将一组命令执行已知的次数,语句基本格式如下
for variable in (list) do command command ... done
ex1,列表for循环中list 列表为常数的情况
#!/bin/bash #使用列表for循环显示5次欢迎操作 for variable in 1 2 3 4 5 do echo "Hello, welcome $variable times " done
[zhangqi@localhost shellscript]$ sh for_ex1.sh Hello, welcome 1 times Hello, welcome 2 times Hello, welcome 3 times Hello, welcome 4 times Hello, welcome 5 times [zhangqi@localhost shellscript]$
ex2,列表为略写形式
#!/bin/bash #使用列表for循环显示5次欢迎操作 for variable in {1..5} do echo "Hello, welcome $variable times " done
[zhangqi@localhost shellscript]$ sh for_ex2.sh Hello, welcome 1 times Hello, welcome 2 times Hello, welcome 3 times Hello, welcome 4 times Hello, welcome 5 times [zhangqi@localhost shellscript]$
上面示例种,我们将1~5进行略写,使其可以正常的与示例1输出相同的结果
ex3,列表为简写形式
#!/bin/bash #使用列表for循环显示5次欢迎操作 for variable in $(seq 1 5) do echo "Hello, welcome $variable times " done
seq 命令是Linux预设的外部命令,一般用于一堆数字的简化写法,可以参考linux常用命令之seq。
执行后,结果同上面相同,就不重复贴出来了。
ex4,按步数跳跃方式实现列表
#!/bin/bash #使用列表for循环显示5次欢迎操作 for variable in {1..5..2} do echo "Hello, welcome $variable times " done
运行下,看下结果
[zhangqi@localhost shellscript]$ sh for_ex4.sh Hello, welcome 1 times Hello, welcome 3 times Hello, welcome 5 times [zhangqi@localhost shellscript]$
ex5,跳跃方式用seq表达
[zhangqi@localhost shellscript]$ cat for_ex5.sh #!/bin/bash #使用列表for循环显示5次欢迎操作 for variable in $(seq 1 2 5) do echo "Hello, welcome $variable times " done [zhangqi@localhost shellscript]$ sh for_ex5.sh Hello, welcome 1 times Hello, welcome 3 times Hello, welcome 5 times [zhangqi@localhost shellscript]$
ex6,用字符串表示列表
[zhangqi@localhost shellscript]$ cat for_ex6.sh #!/bin/bash #使用列表for循环显示周一到周日对应的英文 for day in Monday Tuesday Wednesday Thursday Friday Saturday Sunday do echo "$day" done [zhangqi@localhost shellscript]$ sh for_ex6.sh Monday Tuesday Wednesday Thursday Friday Saturday Sunday [zhangqi@localhost shellscript]$
[zhangqi@localhost shellscript]$ cat for_ex7.sh #!/bin/bash #使用命令打印数组 for variable in `ls /` do echo "Every directory is $variable " done [zhangqi@localhost shellscript]$ sh for_ex7.sh Every directory is bin Every directory is boot Every directory is dev Every directory is etc Every directory is home Every directory is lib Every directory is lost+found Every directory is media Every directory is mnt Every directory is opt Every directory is proc Every directory is root Every directory is sbin Every directory is selinux Every directory is srv Every directory is sys Every directory is tmp Every directory is usr Every directory is var [zhangqi@localhost shellscript]$
这里的命令格式可以使用 $( command) 或 `command`,效果相同,这里就不再做展示了。
ex8,通过脚本传参实现里列表
[zhangqi@localhost shellscript]$ cat for_ex8.sh #!/bin/bash echo "number of arguments is $#" echo "What you input is :" #使用命令打印数组 for argument in "$*" do echo "$argument " done [zhangqi@localhost shellscript]$ sh for_ex8.sh 1 hello shell number of arguments is 3 What you input is : 1 hello shell [zhangqi@localhost shellscript]$
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。