---
title: "我使用过的Linux命令之while - Bash中的While循环"
author: "codingstandards"
date: "2010-10-10T16:52:59Z"
tags: []
categories: ["操作系统"]
slug: "linux-while-bash-while"
draft: false
lastmod: "2011-04-10T11:34:30Z"
id: 780524
url: "https://codingstandards.iteye.com/blog/780524"
---

# 我使用过的Linux命令之while - Bash中的While循环

本文链接：[http://codingstandards.iteye.com/blog/780524](/blog/780524)    （转载请注明出处）

 

## 用途说明

while循环是Shell中常用的语法结构，它与其他编程语言中的while有些类似，只是写法有些不一样罢了。

## 常用格式

### 格式一

while 条件;

do

    语句

done

### 格式二 死循环

while true

do

    语句

done

### 格式三 死循环

while :

do

    语句

done

### 格式四 死循环

while \[ 1 ]

do

    语句

done

### 格式五 死循环

while \[ 0 ]

do

    语句

done

## 使用示例

### 示例一

```
COUNTER=0
while [  $COUNTER -lt 10 ]; do
    echo The counter is $COUNTER
    let COUNTER=COUNTER+1 
done
```

 

\[root@jfht ~]# COUNTER=0  
\[root@jfht ~]# while \[  $COUNTER -lt 10 ]; do  
&gt;     echo The counter is $COUNTER  
&gt;     let COUNTER=COUNTER+1  
&gt; done  
The counter is 0  
The counter is 1  
The counter is 2  
The counter is 3  
The counter is 4  
The counter is 5  
The counter is 6  
The counter is 7  
The counter is 8  
The counter is 9  
\[root@jfht ~]#

 

这个while循环改用for循环更好些

```
for ((COUNTER=0; COUNTER<10; ++COUNTER))
do
    echo The counter is $COUNTER
done

```

 

\[root@jfht ~]# for ((COUNTER=0; COUNTER&lt;10; ++COUNTER))  
&gt; do  
&gt;     echo The counter is $COUNTER  
&gt; done  
The counter is 0  
The counter is 1  
The counter is 2  
The counter is 3  
The counter is 4  
The counter is 5  
The counter is 6  
The counter is 7  
The counter is 8  
The counter is 9  
\[root@jfht ~]#

### 示例二

```
while true
do
    date
    sleep 1
done
```

 

\[root@jfht ~]# while true  
&gt; do  
&gt;     date  
&gt;     sleep 1  
&gt; done  
2010年 10月 10日 星期日 16:35:22 CST  
2010年 10月 10日 星期日 16:35:23 CST  
2010年 10月 10日 星期日 16:35:24 CST  
2010年 10月 10日 星期日 16:35:25 CST  
2010年 10月 10日 星期日 16:35:26 CST  
2010年 10月 10日 星期日 16:35:27 CST  
Ctrl+C  
\[root@jfht ~]#

 

### 示例三 读取输入

```
while read line
do
    echo $line
done
```

 

\[root@jfht ~]# while read line  
&gt; do  
&gt;     echo $line  
&gt; done  
hello  
hello  
world  
worldCtrl+D  
\[root@jfht ~]#

 

### 实例四 处理命令行参数

文件 while\_4.sh

```
#!/bin/sh

usage()
{
    echo "usage: $0 [-a] [-e <admin>] [-f <serverfile>] [-h] [-d <domain>] [-s <whois_server>] [-q] [-x <warndays>]"
}

while getopts ae:f:hd:s:qx: option
do
        case "${option}" in
                a) ALARM="TRUE";;
                e) ADMIN=${OPTARG};;
                d) DOMAIN=${OPTARG};;
                f) SERVERFILE=$OPTARG;;
                s) WHOIS_SERVER=$OPTARG;;
                q) QUIET="TRUE";;
                x) WARNDAYS=$OPTARG;;
                \?) usage; exit 1;;
        esac
done

echo "ALARM=$ALARM"
echo "ADMIN=$ADMIN"
```

 

\[root@jfht ~]# cat while\_4.sh  
#!/bin/sh

usage()  
{  
    echo "usage: $0 \[-a] \[-e &lt;admin&gt;] \[-f &lt;serverfile&gt;] \[-h] \[-d &lt;domain&gt;] \[-s &lt;whois\_server&gt;] \[-q] \[-x &lt;warndays&gt;]"  
}

while getopts ae:f:hd:s:qx: option  
do  
        case "${option}" in  
                a) ALARM="TRUE";;  
                e) ADMIN=${OPTARG};;  
                d) DOMAIN=${OPTARG};;  
                f) SERVERFILE=$OPTARG;;  
                s) WHOIS\_SERVER=$OPTARG;;  
                q) QUIET="TRUE";;  
                x) WARNDAYS=$OPTARG;;  
                \\?) usage; exit 1;;  
        esac  
done

echo "ALARM=$ALARM"  
echo "ADMIN=$ADMIN"

\[root@jfht ~]# chmod +x while\_4.sh  
\[root@jfht ~]# ./while\_4.sh  
ALARM=  
ADMIN=  
\[root@jfht ~]# ./while\_4.sh -a  
ALARM=TRUE  
ADMIN=  
\[root@jfht ~]# ./while\_4.sh -e hy  
ALARM=  
ADMIN=hy  
\[root@jfht ~]#

## 问题思考

1\. 为什么 while \[ 0 ] 也会是死循环？

2\. 怎么使用while read line从文件中读入数据？

## 相关资料

【1】BASH Programming - Introduction HOW-TO [7.3 While sample](http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-7.html#ss7.3)

【2】Audbel [语法单行BASH的无限while循环](http://audbel.com/0/6209821)

【3】一个有很多while示例的英文网页 [Bash While Loop Example](http://www.cyberciti.biz/faq/bash-while-loop/)

 

返回 [我使用过的Linux命令系列总目录](/blog/786653)