在shell编程中经常用到循环,常用的循环有for和while循环两种。while循环默认以行读取文件,而for循环以空格读取文件切分文件,本篇就结合现网的一些使用示例说说二者的用法和区别。
一、常用语法
1、for循环
for循环常用的语法结构有如下几种:
for 变量 in seq字符串
for 变量 in `command` ” “
for 变量 in “$”或“$*”
for((赋值;条件;运算语句))
2、while循环
while循环常用的语法结构有如下几种:
while [ $i
for 命令:
for i in 的各种用法 :
for i in “file1” “file2” “file3”
for i in /boot/*
for i in /etc/*.conf
for i in $(seq -w 10) –》等宽的01-10
for i in {1…10}
for i in $( ls )
for I in $(< file)
for i in “$” –》取所有位置参数,可简写为for i
注意:bash shell支持C