Linux shell中while循环是怎样的

发布时间:2022-01-26 16:46:33 作者:柒染
来源:亿速云 阅读:336

今天就跟大家聊聊有关Linux shell中while循环是怎样的,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

在shell中while循环也是一个常用的循环结构,和其他语言的语法有许多类似之处,但是也有个别地方不一样。

常用格式

格式一

while 条件;

do

 语句

done

格式二 死循环

while true

do

 语句

done

格式三 死循环

while :

do

 语句

done

格式四 死循环

while [ 1 ]

do

 语句

done

格式五 死循环

while [ 0 ]

do

 语句

done

使用示例

示例一

Bash代码  Linux shell中while循环是怎样的

  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代码  Linux shell中while循环是怎样的

  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代码  Linux shell中while循环是怎样的

  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代码  


#!/bin/sh 
usage() 
{ 
  echo “usage: $0 [-a] [-e ] [-f ] [-h] [-d ] [-s] [-q] [-x]” 
} 
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 ] [-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

看完上述内容,你们对Linux shell中while循环是怎样的有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

推荐阅读:
  1. Linux丨shell语句while和until和case使
  2. 好程序员Linux云计算教程分享Shell脚本面试题

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

linux shell while

上一篇:Linux怎么安装autojump

下一篇:@Transactional注解怎么用

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》