摘要
在BASH编程中流程控制主要有三种方式,顺序执行、选择执行、循环执行,其中选择执行有if,循环执行有for、while、until,今天就来说说循环执行的while、until、for
首先我们来说说while和until,其实while和until是没什么区别的,弄懂了一个另外一个自然而然的懂了。
for:遍历有限的元素列表来实现循环执行,特别适用于循环次数已知的场景
while:主要用于循环次数未知需要特定条件来限定的场景
untile:类似于while
for循环:
for语句的格式
for 变量 in 列表; do
循环体,也就是要打算执行的语句
done
(注意这里的变量是做赋值的不是做引用,引用是要加$符号的,赋值是不加$符号的)
(这里的LIST怎么生成就看具体情况自来生成了)
(do如果和for在同一行do前要加;号)(done表示循环体结束)
整个for语句实现的功能
LIST:列表中间包含一个或者多个元素
每一次使用列表中的一个元素赋值给变量,执行一次循环体,当循环体执行完以后再次回到元素赋值阶段,直到所有元素被遍历完毕,循环才算结算
退出条件:遍历结束
生成数值列表:
{start..end}:
例如:{1..99}就生成了1,2,3...99的列表了,这是一个命令行展开语句,自动展开大括号里面的内容
seq命令:
这里说说seq的用法:
seq [option]...结束值,如果只有一个结束值,那么起始值是1步进值是1
seq [option]...起始值 结束值,如果有起始值和结束值那么步进值是1
seq [option]...起始值 步进值 结束值,这个就是指定起始结束和步进值了
for i in `seq 1 10`;do echo $i;done
for ((变量=初始值; 条件判断; 变量变化))
循环体,也就是要打算执行的语句
done
for ((i=1;i<=10;i+=2));do echo $i;done
for循环的缺点:需要借助列表,如果列表太大或者列表未知那么for就有点悲催了。
for循环的有点:循环构建简单,上手容易。
栗子:(下面测试的两种写法其实是一样的)
[root@www.dwhd.org /tmp/for]# for i in {1..9}; do echo "`date` this is $i";done
2015年 05月 27日 星期三 10:52:34 CST this is 1
2015年 05月 27日 星期三 10:52:34 CST this is 2
2015年 05月 27日 星期三 10:52:34 CST this is 3
2015年 05月 27日 星期三 10:52:34 CST this is 4
2015年 05月 27日 星期三 10:52:34 CST this is 5
2015年 05月 27日 星期三 10:52:34 CST this is 6
2015年 05月 27日 星期三 10:52:34 CST this is 7
2015年 05月 27日 星期三 10:52:34 CST this is 8
2015年 05月 27日 星期三 10:52:34 CST this is 9
[root@www.dwhd.org /tmp/for]# for i in {1..9}
> do
> echo "`date` this is $i"
> done
2015年 05月 27日 星期三 10:53:48 CST this is 1
2015年 05月 27日 星期三 10:53:48 CST this is 2
2015年 05月 27日 星期三 10:53:48 CST this is 3
2015年 05月 27日 星期三 10:53:48 CST this is 4
2015年 05月 27日 星期三 10:53:48 CST this is 5
2015年 05月 27日 星期三 10:53:48 CST this is 6
2015年 05月 27日 星期三 10:53:48 CST this is 7
2015年 05月 27日 星期三 10:53:48 CST this is 8
2015年 05月 27日 星期三 10:53:48 CST this is 9
[root@www.dwhd.org /tmp/for]#
[root@www.dwhd.org /tmp/for]# for ((i=1;i<5;i+=1));do echo "`date` this is $i";done
2015年 05月 27日 星期三 11:27:17 CST this is 1
2015年 05月 27日 星期三 11:27:17 CST this is 2
2015年 05月 27日 星期三 11:27:17 CST this is 3
2015年 05月 27日 星期三 11:27:17 CST this is 4
[root@www.dwhd.org /tmp/for]# for ((i=1;i<5;i++));do echo "`date` this is $i";done
2015年 05月 27日 星期三 11:27:27 CST this is 1
2015年 05月 27日 星期三 11:27:27 CST this is 2
2015年 05月 27日 星期三 11:27:27 CST this is 3
2015年 05月 27日 星期三 11:27:27 CST this is 4
[root@www.dwhd.org /tmp/for]#
seq的简单用法演示:
[root@www.dwhd.org /tmp/for]# seq 3
1
2
3
[root@www.dwhd.org /tmp/for]# seq 1 3
1
2
3
[root@www.dwhd.org /tmp/for]# seq 1 3 10
1
4
7
10
[root@www.dwhd.org /tmp/for]#
[root@www.dwhd.org /tmp/for]# for i in `seq 5`; do echo "`date` this is $i";done
2015年 05月 27日 星期三 11:10:57 CST this is 1
2015年 05月 27日 星期三 11:10:57 CST this is 2
2015年 05月 27日 星期三 11:10:57 CST this is 3
2015年 05月 27日 星期三 11:10:57 CST this is 4
2015年 05月 27日 星期三 11:10:57 CST this is 5
[root@www.dwhd.org /tmp/for]#
while循环:
while适用与次数未知或不方便试用for直接生成较大列表时;
while 测试条件;do
循环体
done
(当测试条件为"真"时,则进入循环,直到测试条件为"假"时退出循环)
例如:
declare -i i=0
while $count <=1000; do
循环体
let i++
done
这里说下i++和++i的区别,为了编译记忆理解,我是开始是这样记的
i++,i在前面就是先等后加,再通俗点说a=i++,那就是a=i;i=i+1
++i,i在后面就是先加后等,通俗说就是i=i+1;a=i
这样就好理解了对吧,看+号位置
untile循环:
其实until很好理解和记的,只需要把测试条件那反过来记就ok了
untile 测试条件;do
循环体
done
(当测试结果是"假"则进入循环,当测试结果是"真"时则退出循环)
由于until在绝大多数情况下都可以被while替代使用,所在实际应用中until见的还是比较少的。也是大家都习惯了while了吧
1、while循环演示
[root@www.dwhd.org /tmp/for]# cat while.sh
#!/bin/bash
#########################################################################
# File Name: while.sh
# Author: LookBack
# Email: admin#05hd.com
# Version:
# Created Time: 2015年05月27日 星期三 13时39分49秒
#########################################################################
declare -i i=1 #这里的作用是指定i为一个数值 值为1
while [ $i -le 3 ];do
echo $i
let i++
done
[root@www.dwhd.org /tmp/for]# bash -x while.sh
+ declare -i i=1
+ '[' 1 -le 3 ']'
+ echo 1
1
+ let i++
+ '[' 2 -le 3 ']'
+ echo 2
2
+ let i++
+ '[' 3 -le 3 ']'
+ echo 3
3
+ let i++
+ '[' 4 -le 3 ']'
[root@www.dwhd.org /tmp/for]#
[root@www.dwhd.org /tmp/for]# cat until.sh
#!/bin/bash
#########################################################################
# File Name: until.sh
# Author: LookBack
# Email: admin#05hd.com
# Version:
# Created Time: 2015年05月27日 星期三 13时56分54秒
#########################################################################
declare -i i=1
until [ $i -gt 3 ];do
echo $i
let i++
done
[root@www.dwhd.org /tmp/for]#
[root@www.dwhd.org /tmp/for]# bash -x until.sh
+ declare -i i=1
+ '[' 1 -gt 3 ']'
+ echo 1
1
+ let i++
+ '[' 2 -gt 3 ']'
+ echo 2
2
+ let i++
+ '[' 3 -gt 3 ']'
+ echo 3
3
+ let i++
+ '[' 4 -gt 3 ']'
[root@www.dwhd.org /tmp/for]#