码迷,mamicode.com
首页 > 系统相关 > 详细

linux中的while命令

时间:2014-11-07 15:07:25      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:while   linux   

while命令有点象if-then和for循环的结合。while测试命令返回0退出状态,就循环一组命令。

while基本格式:

while test command

do 

  other commands

done

示例:

#!/bin/bash

varl=5

while [ $varl -gt 0 ]

do

 echo $varl

 varl=$[ $varl -1 ]

done

[root@localhost ~]# ./test10.sh 

5

4

3

2

1

注意:因为while是检测到退出状态为0就执行,只有让输出为非0才会停止循环,

 varl=$[ $varl -1 ]得出的结果为非0


使用多条测试命令

#!/bin/bash

var1=3

while echo $var1

[ $var1 -ge 0 ]

do

 echo "The is inside the loop"

 var1=$[ $var1 -1 ]

done

[root@localhost ~]# ./test11.sh 

3

The is inside the loop

2

The is inside the loop

1

The is inside the loop

0

The is inside the loop

-1

在多行命令中,所有的测试命令都在每次失代中执行,包含测试命令失败的最后一次失代



linux中的while命令

标签:while   linux   

原文地址:http://liangey.blog.51cto.com/9097868/1573946

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!