linux中循环语句for,while,until用法

循环语句:
    Bash Shell中主要提供了三种循环方式:for、while和until。
    for循环声明格式:

 代码如下 复制代码
    for variable in word_list
    do
        command
    done

    见如下示例脚本:
   

 代码如下 复制代码
/> cat > test7.sh
    for score in math english physics chemist   #for将循环读取in后面的单词列表,类似于Java的for-each。
    do
        echo "score = $score"
    done
    echo "out of for loop"
    CTRL+D
    /> . ./test7.sh
    score = math
    score = english
    score = physics
    score = chemist
    out of for loop
   
    /> cat > mylist   #构造数据文件
    tom
    patty
    ann
    jake
    CTRL+D
    /> cat > test8.sh
    #!/bin/sh
    for person in $(cat mylist)                 #for将循环读取cat mylist命令的执行结果。
    do
            echo "person = $person"
    done
    echo "out of for loop."
    CTRL+D
    /> . ./test8.sh
    person = tom
    person = patty
    person = ann
    person = jake
    out of for loop.
   
    /> cat > test9.sh
    for file in test[1-8].sh                        #for将读取test1-test8,后缀为.sh的文件
    do
            if [ -f $file ]                              #判断文件在当前目录是否存在。
            then
                    echo "$file exists."
            fi
    done
    CTRL+D
    /> . ./test9.sh
    test2.sh exists.
    test3.sh exists.
    test4.sh exists.
    test5.sh exists.
    test6.sh exists.
    test7.sh exists.
    test8.sh exists.
   
    /> cat > test10.sh
    for name in $*                                  #读取脚本的命令行参数数组,还可以写成for name的简化形式。
    do
            echo "Hi, $name"
    done
    CTRL+D
    /> . ./test10.sh stephen ann
    Hi, stephen
    Hi, ann

    
    while循环声明格式:
   

 代码如下 复制代码
while command  #如果command命令的执行结果为0,或条件判断为真时,执行循环体内的命令。
    do
        command
    done

    见如下示例脚本:
   

 代码如下 复制代码
/> cat > test1.sh   
    num=0
    while (( num < 10 ))               #等同于 [ $num -lt 10 ]
    do
            echo -n "$num "
            let num+=1
    done
    echo -e "nHere's out of loop."
    CTRL+D
    /> . ./test1.sh
    0 1 2 3 4 5 6 7 8 9
    Here's out of loop.
   
    /> cat > test2.sh
    go=start
    echo Type q to quit.
    while [[ -n $go ]]                     #等同于[ -n "$go" ],如使用该风格,$go需要被双引号括起。
    do
            echo -n How are you.
            read word
            if [[ $word == [Qq] ]]      #等同于[ "$word" = Q -o "$word" = q ]
            then
                    echo Bye.
                    go=                        #将go变量的值置空。
            fi
    done
    CTRL+D
    /> . ./test2.sh
    How are you. Hi
    How are you. q
    Bye.

    
    until循环声明格式:
    until command                         #其判断条件和while正好相反,即command返回非0,或条件为假时执行循环体内的命令。
    do
        command
    done
    见如下示例脚本:
    /> cat > test3.sh
    until who | grep stephen           #循环体内的命令将被执行,直到stephen登录,即grep命令的返回值为0时才退出循环。

 代码如下 复制代码
    do
            sleep 1
            echo "Stephen still doesn't login."
    done
    CTRL+D
时间: 2024-08-31 08:18:01

linux中循环语句for,while,until用法的相关文章

Linux中循环语句while和until的区别

1 while[满足条件就执行] #!/bin/bash sum=0 i=1; #当i<=100时执行while [ $i -le 100 ]         do                 sum=$(($sum+$i))                 i=$(($i+1))         done               echo "the sum is $sum" 2 until[满足条件就不执行] #!/bin/bash sum=0 i=1 #当i>1

python中循环语句while用法实例

  本文实例讲述了python中循环语句while用法.分享给大家供大家参考.具体分析如下: 对于python的while语句,注意其缩进即可. python和其他语言一样也有break和continue,分别用来表示跳出循环和继续循环. ? 1 2 3 4 5 6 7 8 #!/usr/bin/python # Simple while loop a = 0 while a < 15: print a, # 在print a后面加,不换行 if a == 10: print "made

详解Python中循环语句的嵌套使用

  这篇文章主要介绍了举例详解Python中循环语句的嵌套使用,是Python入门中的基础知识,需要的朋友可以参考下 Python编程语言允许在一个循环内嵌套另一个循环.下面将介绍几个例子来说明这一概念. 语法 在Python中嵌套循环语句的语法如下: ? 1 2 3 4 for iterating_var in sequence: for iterating_var in sequence: statements(s) statements(s) Python编程语言允许在一个循环内嵌套另一个

VB6.0中For语句的两种用法,如何用第二种用法遍历一个数组?

问题描述 VB6.0中For语句的两种用法,如何用第二种用法遍历一个数组? VB6.0中For语句的两种用法,如何用第二种用法遍历一个数组? 解决方案 正序 for i = lbound(arr) to ubound(arr) arr(i) next 逆序 for i = ubound(arr) to lbound(arr) step -1 arr(i) next 解决方案二: 一个加密解密函数 for vb6.0!VB6.0中的编译预处理--#IF语句

linux中mount/umount命令的基本用法及开机自动挂载方法_Linux

本文介绍了linux中mount/umount命令的基本用法及开机自动挂载,具体方法如下: mount命令格式如下: 格式:mount [-参数] [设备名称] [挂载点] 其中常用的参数有: -a 安装在/etc/fstab文件中类出的所有文件系统. -f 伪装mount,作出检查设备和目录的样子,但并不真正挂载文件系统. -n 不把安装记录在/etc/mtab 文件中. -r 讲文件系统安装为只读. -v 详细显示安装信息. -w 将文件系统安装为可写,为命令默认情况. -t <文件系统类型

Swift中循环语句中的转移语句 break 和 continue_Swift

下面通过实例代码给大家介绍了Swift中循环语句中的转移语句 break 和 continue,具体代码如下所示: /** 循环语句中的转移语句 break 和 continue */ let array:Array = [3, 4, 5, 6, 7, 8, 9] for k in array { if k == 5 { print(k) break } } print("--------->") for k in array { if k == 5 { // 结束本次循环,进入

几种有用的变型 PHP中循环语句的用法

for语句可以说是PHP(同时也是多种语言)的循环控制部份最基本的一个语句了,for语句的执行规律和基础用法在这里就不多说,可以参见PHP手册for语句部分.PHP手册中对它的语法定义如下: for (expr1; expr2; expr3) statement 下面说说for语句几种有用的变型. 1.无限循环 首先是人尽皆知的无限循环(亦可称"死循环").由于空表达式null在语法上是有效的,所以我们可以把for语句的三个表达式留空,这样就会产生不断执行for嵌套语句的效果. for

几种有用的变型 PHP中循环语句的用法介绍_php技巧

PHP手册中对它的语法定义如下: for (expr1; expr2; expr3) statement 下面说说for语句几种有用的变型. 1.无限循环 首先是人尽皆知的无限循环(亦可称"死循环").由于空表达式null在语法上是有效的,所以我们可以把for语句的三个表达式留空,这样就会产生不断执行for嵌套语句的效果. for (;;) { //放置需要不断执行的语句 } ?> 虽然有一些任务会使用到无限循环,但是大多数程序任务,特别是PHP所能涉及的领域,在使用无限循环时都

bash 编程中循环语句用法_linux shell

1.if 是单分支语句,使用格式如下: if condition ; then statement -.. fi 2.if - else 是双分支语句,使用格式如下: if condition ; then statement -. else statement -. fi 3.if -elif-elif-else 是多分支语句,使用格式如下: if condition ; then statement -. elif condition ; then statement -.. elif co