for 循环的基本语法:
for VAR in item1 item2 ... itemN
do
command1
command2
#...
#...
commandN
done
for 循环读取变量的内容 的语法:
for VAR in $varName
do
command1
command2
#...
#...
commandN
done
for 循环命令替换的语法:
for VAR in $(Linux-command-name) # 或 for VAR in `Linux-command-name`
do
command1
command2
#...
#...
commandN
done
for 循环除了基本的语法,还可以使用三项表达式语法:
for (( EXP1; EXP2; EXP3 ))
do
command1
command2
...
...
commandN
done
另外,for 循环是可以进行嵌套使用的,其语法形式:
for (( EXP1; EXP2; EXP3 ))
do
command1
command2
#...
for (( EXP1; EXP2; EXP3 ))
do
commandInternal1
commandInternal2
#...
#...
commandInternalN
done
#...
commandN
done
下面是简单的使用示例:
本文参考自 《Linux Shell命令行及脚本编程实例详解 》