for循环示例
for循环语法:
| 1 | for VARIABLE in 1 2 3 4 5 .. N |
| 07 | echo "Welcome $i times" |
bash version 3.0+版本
bash version 4版本
| echo "Bash version ${BASH_VERSION}..." |
含有“seq”命令的语法示例
for循环的三个表达式
语法如下:
| for (( EXP1; EXP2; EXP3 )) |
示例如下:
| echo "Welcome $c times..." |
效果:
for的无限循环
| echo "infinite loops [ hit CTRL+C to stop]" |
break条件语句
| statements1 #Executed for all values of ''I'', up to a disaster-condition if any. |
| statements3 #While good and, no disaster-condition. |
下面的shell脚本将通过在/ etc目录中存储的所有文件。 for循环将放弃当/ etc / resolv.conf的文件中找到。
| if [ "${file}" == "/etc/resolv.conf" ] |
| countNameservers=$(grep -c nameserver /etc/resolv.conf) |
| echo "Total ${countNameservers} nameservers defined in ${file}" |
continue条件语句
| statements1 #Executed for all values of ''I'', up to a disaster-condition if any. |
| continue #Go to next iteration of I in the loop and skip statements3 |
利用这个脚本在命令行中指定的所有文件名的备份。如果。bak文件存在,它会跳过cp命令。
| # if .bak backup file exists, read next file |
| echo "Skiping $f file..." |
| continue # read next file and skip cp command |
| # we are hear means no backup file exists, just use cp command to copy file |
阅读(1844) | 评论(0) | 转发(0) |