for循环是Linux shell 中最常用的结构。for 循环有三种结构:一种结构是列表for循环;第二种结构是不带列表for循环;第三种结构是类C风格的for循环。
本篇博文重点看列表for循环,列表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
[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]$