Shell for循环
2015-12-29 14:27
337 查看
与其他编程语言类似,Shell支持for循环。
for循环一般格式为:
列表是一组值(数字、字符串等)组成的序列,每个值通过空格分隔。每循环一次,就将列表中的下一个值赋给变量。
in 列表是可选的,如果不用它,for 循环使用命令行的位置参数。
例如,顺序输出当前列表中的数字:
运行结果:
顺序输出字符串中的字符:
运行结果:
显示主目录下以 .bash 开头的文件:
运行结果:
for循环一般格式为:
for 变量 in 列表 do command1 command2 ... commandN done
列表是一组值(数字、字符串等)组成的序列,每个值通过空格分隔。每循环一次,就将列表中的下一个值赋给变量。
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
显示主目录下以 .bash 开头的文件:
复制纯文本新窗口
#!/bin/bash for FILE in $HOME/.bash* do echo $FILE done
运行结果:
/root/.bash_history /root/.bash_logout /root/.bash_profile /root/.bashrc
相关文章推荐
- Shell case esac语句
- Shell if else语句
- Shell if else语句
- shell printf命令:格式化输出语句
- Shell echo命令
- Shell数组:shell数组的定义、数组长度
- Shell字符串
- Shell运算符:Shell算数运算符、关系运算符、布尔运算符、字符串运算符等
- Shell替换:Shell变量替换,命令替换,转义字符
- shell表达式
- Shell特殊变量:Shell $0, $#, $*, $@, $?, $$和命令行参数
- 父Shell与子Shell
- #! /bin/sh 与 #! /bin/bash 的区别
- 使用gitshell把工程推送到git
- Android Shell命令之dumpsys部分功能简介
- 02. Shell文件权限和脚本执行(Linux权限基础, 用户组)
- shell脚本中的数学运算
- 在命令行中通过adb shell am broadcast发送广播通知
- node_shell_cmd
- ubuntu的bashrc文件