Linux命令之while - Bash中的While循环
来源:互联网 发布:做市商制度 知乎 编辑:程序博客网 时间:2024/06/06 03:09
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
COUNTER=0while [ $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
> echo The counter is $COUNTER
> let COUNTER=COUNTER+1
> 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
for ((COUNTER=0; COUNTER<10; ++COUNTER))do echo The counter is $COUNTERdone
[root@jfht ~]# for ((COUNTER=0; COUNTER<10; ++COUNTER))
> do
> echo The counter is $COUNTER
> 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
while truedo date sleep 1done
[root@jfht ~]# while true
> do
> date
> sleep 1
> 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
while read linedo echo $linedone
[root@jfht ~]# while read line
> do
> echo $line
> 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"
#!/bin/shusage(){ echo "usage: $0 [-a] [-e <admin>] [-f <serverfile>] [-h] [-d <domain>] [-s <whois_server>] [-q] [-x <warndays>]"}while getopts ae:f:hd:s:qx: optiondo 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;; esacdoneecho "ALARM=$ALARM"echo "ADMIN=$ADMIN"
[root@jfht ~]# cat 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 ~]# 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从文件中读入数据?
- Linux命令之while - Bash中的While循环
- Linux命令之while - Bash中的While循环
- 我使用过的Linux命令之while - Bash中的While循环
- Bash中的While循环
- linux之while循环
- 【bash】编程之for,while,until循环
- Linux之Shell脚本while循环ssh登录执行命令
- linux 命令系列之 while循环和until循 (63)
- 玩转Bash脚本:循环结构之while循环
- Bash shell,while 循环中的变量无法保存问题
- MYSQL 循环命令while
- 关于while read line循环的linux bash shell bug
- PHP中的while循环
- javascript中的while循环
- javascript中的while循环
- Shell中的while循环
- python中的while循环
- Shell中的while循环
- Android-应用程序签名及发布
- Rust中文翻译9
- 什么是NTFS文件系统
- C/C++中的输入与输出及如何读取一行文本
- thinkphp解决无刷新分页搜索问题
- Linux命令之while - Bash中的While循环
- 面试记录
- 稀疏矩阵乘法加法等的java实现
- opencv各种画图 直线 矩形 圆 椭圆
- java多线程实例-模拟购票
- 双系统安装过程
- NSInteger 与 NSUInteger 和 int 区别
- Hadoop第十讲
- hdu2955(01背包)