良许Linux教程网 干货合集 Linux shell中while循环详解

Linux shell中while循环详解

在shell中while循环也是一个常用的循环结构,和其他语言的语法有许多类似之处,但是也有个别地方不一样,下面良许教程网为大家分享一下shell中while循环的具体使用方法。

Linux shell中while循环详解

常用格式

格式一

while 条件;

do

 语句

done

格式二 死循环

while true

do

 语句

done

格式三 死循环

while :

do

 语句

done

格式四 死循环

while [ 1 ]

do

 语句

done

格式五 死循环

while [ 0 ]

do

 语句

done

使用示例

示例一

Bash代码  收藏代码

  1. COUNTER=0

  2. while [ $COUNTER -lt 10 ]; do

  3.  echo The counter is $COUNTER

  4.  let COUNTER=COUNTER+1  

  5. done

[root@jfht ~]# COUNTER=0[root@jfht ~]# while [ COUNTER>   let COUNTER=COUNTER+1> doneThe counter is 0The counter is 1The counter is 2The counter is 3The counter is 4The counter is 5The counter is 6The counter is 7The counter is 8The counter is 9[root@jfht ~]#

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

Bash代码  收藏代码

  1. for ((COUNTER=0; COUNTER

  2. do

  3.  echo The counter is $COUNTER

  4. done

[root@jfht ~]# for ((COUNTER=0; COUNTER> do>   echo The counter is $COUNTER> doneThe counter is 0The counter is 1The counter is 2The counter is 3The counter is 4The counter is 5The counter is 6The counter is 7The counter is 8The counter is 9[root@jfht ~]#

示例二

Bash代码  收藏代码

  1. while true

  2. do

  3.  date

  4.  sleep 1

  5. done

[root@jfht ~]# while true> do>   date>   sleep 1> done2010年 10月 10日 星期日 16:35:22 CST2010年 10月 10日 星期日 16:35:23 CST2010年 10月 10日 星期日 16:35:24 CST2010年 10月 10日 星期日 16:35:25 CST2010年 10月 10日 星期日 16:35:26 CST2010年 10月 10日 星期日 16:35:27 CSTCtrl+C[root@jfht ~]#

示例三 读取输入

Java代码  收藏代码

  1. while read line

  2. do

  3.  echo $line

  4. done

[root@jfht ~]# while read line> do>   echo $line> donehellohelloworldworldCtrl+D[root@jfht ~]#

实例四 处理命令行参数

文件 while_4.sh

Bash代码  收藏代码

  1. #!/bin/sh

  2. usage()

  3. {

  4.  echo “usage: $0 [-a] [-e ] [-f ] [-h] [-d ] [-s ] [-q] [-x ]”

  5. }

  6. while getopts ae:f:hd:s:qx: option

  7. do

  8.    case “${option}” in

  9.        a) ALARM=”TRUE”;;

  10.        e) ADMIN=${OPTARG};;

  11.        d) DOMAIN=${OPTARG};;

  12.        f) SERVERFILE=$OPTARG;;

  13.        s) WHOIS_SERVER=$OPTARG;;

  14.        q) QUIET=”TRUE”;;

  15.        x) WARNDAYS=$OPTARG;;

  16.        \?) usage; exit 1;;

  17.    esac

  18. done

  19. echo “ALARM=$ALARM”

  20. echo “ADMIN=$ADMIN”

[root@jfht ~]# cat while_4.sh#!/bin/sh

usage(){  echo “usage: $0 [-a] [-e ] [-f ] [-h] [-d ] [-s ] [-q] [-x ]”}

while getopts ae:f:hd:s:qx: optiondo    case “{OPTARG};;        d) DOMAIN=OPTARG;;        s) WHOIS_SERVER=OPTARG;;        \?) usage; exit 1;;    esacdone

echo “ALARM=ADMIN”

[root@jfht ~]# chmod +x while_4.sh[root@jfht ~]# ./while_4.shALARM=ADMIN=[root@jfht ~]# ./while_4.sh -aALARM=TRUEADMIN=[root@jfht ~]# ./while_4.sh -e hyALARM=ADMIN=hy[root@jfht ~]#

至此关于shell中while循环的相关内容分享结束,大家有任何问题都可以通过评论区将问题提交给我们。

以上就是良许教程网为各位朋友分享的Linux系统相关内容。想要了解更多Linux相关知识记得关注公众号“良许Linux”,或扫描下方二维码进行关注,更多干货等着你!

137e00002230ad9f26e78-265x300

本文由 良许Linux教程网 发布,可自由转载、引用,但需署名作者且注明文章出处。如转载至微信公众号,请在文末添加作者公众号二维码。
良许

作者: 良许

良许,世界500强企业Linux开发工程师,公众号【良许Linux】的作者,全网拥有超30W粉丝。个人标签:创业者,CSDN学院讲师,副业达人,流量玩家,摄影爱好者。
上一篇
下一篇

发表评论

联系我们

联系我们

公众号:良许Linux

在线咨询: QQ交谈

邮箱: yychuyu@163.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部