Shell脚本for循环语句简明教程
与其他编程语言类似,Shell支持for循环。
for循环一般格式为:
for变量名in列表 do command1 command2 ... commandN done
当变量值在列表里,for循环即执行一次所有命令,使用变量名获取列表中的当前取值。命令可为任何有效的shell命令和语句。in列表可以包含替换、字符串和文件名。
in列表是可选的,如果不用它,for循环使用命令行的位置参数。
例如,顺序输出当前列表中的数字:
forloopin12345 do echo"Thevalueis:$loop" done
输出:
Thevalueis:1
Thevalueis:2
Thevalueis:3
Thevalueis:4
Thevalueis:5
顺序输出字符串中的字符:
forstrin'Thisisastring' do echo$str done
输出:
Thisisastring