您的位置:首页 > 运维架构 > Shell

Shell脚本中实现循环的方式

2018-02-12 08:35 441 查看

shell脚本中实现循环的方式

源码如下:

1) 使用C语言风格版的for循环

#!/bin/bash
#the way of  circulation in  shell
#1.the c-style for circulation in linux
#for((i = 1;i < 10;i++))
#do
#echo the current i is:$i
#done


2) 使用while实现循环—-包括三种循环变量自增的方式

#the way 2 to do circulation
i=1
while [ $i -le 10 ]
do
echo the current i is :$i
let i++
#let i=$i+1
#let i=i+1
#i=$[ $i + 1 ]
done


3) 使用until实现循环

需要注意的是:这里的until循环的终止条件是“当条件为真时,终止回话。”

#!/bin/bash
#using the until command
var1=1
until [ $var1 -eq 10 ]
do
echo $var1
let var1++
done
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: