您现在的位置是:网站首页> 编程资料编程资料
Shell脚本for循环语句简明教程_linux shell_
2023-05-26
512人已围观
简介 Shell脚本for循环语句简明教程_linux shell_
与其他编程语言类似,Shell支持for循环。
for循环一般格式为:
复制代码 代码如下:
for 变量名 in 列表
do
command1
command2
...
commandN
done
当变量值在列表里,for循环即执行一次所有命令,使用变量名获取列表中的当前取值。命令可为任何有效的shell命令和语句。in列表可以包含替换、字符串和文件名。
in列表是可选的,如果不用它,for循环使用命令行的位置参数。
例如,顺序输出当前列表中的数字:
复制代码 代码如下:
for loop in 1 2 3 4 5
do
echo "The value is: $loop"
done
输出:
The value is: 1
The value is: 2
The value is: 3
The value is: 4
The value is: 5
顺序输出字符串中的字符:
复制代码 代码如下:
for str in 'This is a string'
do
echo $str
done
输出:
This is a string
您可能感兴趣的文章:
相关内容
- Shell脚本case语句简明教程_linux shell_
- Shell脚本if else语句小结_linux shell_
- Shell脚本test命令使用总结和实例_linux shell_
- Shell脚本echo指令使用小技巧_linux shell_
- Shell脚本数组操作小结_linux shell_
- Shell脚本字符串单引号和双引号的区别浅析_linux shell_
- Shell脚本注释写法_linux shell_
- Shell脚本定义变量和重新赋值_linux shell_
- 什么是Shell?Shell脚本基础知识详细介绍_linux shell_
- 一键配置CentOS iptables防火墙的Shell脚本分享_linux shell_
