for循环是Linux shell中最罕用的构造。
for 循环有三种构造:一种构造是列表for循环;第二种构造是不带列表for循环;第三种构造是类C格调的for循环。
本篇博文重点看列表for循环,列表for循环大的格局固定,在列表形成上分多种情景,如数字列表、字符串列表、命令列表、脚本传参列表等,上面一一来看。
更具体的对于Linux如何应用bash for 循环
列表for循环语句用于将一组命令执行已知的次数,语句根本格局如下
`
for variable in (list)do command command done
`
其中,do 和 done之间的命令成为循环体,执行次数和list列表中常数或字符串的个数雷同。当执行for循环时,首先将in 后 list 列表的第一个常数或字符串赋给循环变量,而后执行循环体;接着将list 列表中的第二个常数或字符串赋值给循环变量,再次执行循环体。这个过程将始终继续到list 列表中无其它常数或字符串,而后执行done命令后的命令序列。
ex1,列表for循环中list 列表为常数的状况
`#!/bin/bash#应用列表for循环显示5次欢送操作for variable in 1 2 3 4 5 do echo "Hello, welcome $variable times "done`
这种示例的循环常常用于计数,范畴被限定在1~5之间。如下是脚本执行后果,因为in 前面列表列出了5个参数,能够看出脚本执行5次欢送操作。
[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]$
Linux shell中反对列表for 循环中应用略写的计数形式,咱们将脚本略作改良
ex2,列表为略写模式
#!/bin/bash#应用列表for循环显示5次欢送操作for variable in {1..5} do echo "Hello, welcome $variable times "done
执行后,后果同脚本1雷同
[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
in {1..5..2} 实现1~5之内的数字,依照步数2进行跳跃
运行下,看下后果
[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]$
ex7、应用命令示意列表
[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/bashecho "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]$
能够看出,参数列表能够是数字,也能够是字符串,然而输出是以空格进行分隔的,如果存在空格,脚本执行时会认为存在另一个参数。
更多内容:
什么是Linux
老手必看的30个Linux命令
Linux常用命令及应用办法