zoukankan      html  css  js  c++  java
  • shell基础(八)-循环语句

       国庆过后;感觉有点慵懒些了;接着上篇;我们继续来学习循环语句。

        一. for循环

            与其他编程语言类似,Shell支持for循环。

    for循环一般格式为:
    for 变量 in 列表
    do
        command1
        command2
        ...
        commandN
    done
    

       列表是一组值(数字、字符串等)组成的序列,每个值通过空格分隔。每循环一次,就将列表中的下一个值赋给变量

       例如,顺序输出当前列表中的数字

    for01.sh
    $ cat for01.sh 
    #!/bin/sh
    for i in 1 2 3 4 5
    do
     echo "this is $i"
    done
    $ ./for01.sh 
    this is 1
    this is 2
    this is 3
    this is 4
    this is 5
    

     当然也可以向其他语言那样for ((i=1;i++<5));但是是要双括号;这个是与众不同。

    #!/bin/sh
    for ((i=1;i<=5;i++))
    do
     echo "this is $i"
    done
    

     【注意】in 列表是可选的,如果不用它,for 循环使用命令行的位置参数。如下:

    $ cat for01.sh 
    #!/bin/sh
    for i
    do
     echo "this is $i"
    done
    $ ./for01.sh 1 2 3 4 5  
    this is 1
    this is 2
    this is 3
    this is 4
    this is 5
    

     【note】对于列表;像上面一样;其实命令ls当前目录下的所有文件就是一个列表


       二.while 循环

    while循环用于不断执行一系列命令,也用于从输入文件中读取数据;命令通常为测试条件

    #其格式为:
    while command
    do
       Statement(s) to be executed if command is true
    done
    

     命令执行完毕,控制返回循环顶部,从头开始直至测试条件为假。
    以for循环的例子。

    $ cat while01.sh 
    #!/bin/sh
    i=0
    while [ $i -lt 5 ]
    do
     let "i++"
     echo "this is $i"
    done
    $ ./while01.sh 
    this is 1
    this is 2
    this is 3
    this is 4
    this is 5
    

     其实while循环用的最多是用来读文件。

    #!/bin/bash
    count=1    
    cat test | while read line        #cat 命令的输出作为read命令的输入,read读到的值放在line中
    do
       echo "Line $count:$line"
       count=$[ $count + 1 ]          
    done
    或者如下
    #!/bin/sh
    count=1
    while read line
    do 
      echo "Line $count:$line"
       count=$[ $count + 1 ]  
    done < test
    

     【注意】当然你用awk的话;那是相当简单;awk '{print "Line " NR " : " $0}' test
    输出时要去除冒号域分隔符,可使用变量IFS。在改变它之前保存IFS的当前设置。然后在脚本执行完后恢复此设置。使用IFS可以将域分隔符改为冒号而不是空格或tab键

    例如文件worker.txt
    Louise Conrad:Accounts:ACC8987
    Peter Jamas:Payroll:PR489
    Fred Terms:Customer:CUS012
    James Lenod:Accounts:ACC887
    Frank Pavely:Payroll:PR489
    while02.sh如下:
    #!/bin/sh
    #author: li0924
    #SAVEIFS=$IFS
    IFS=:
    while read name dept id
     do
      echo -e "$name	$dept	$id"
     done < worker.txt
    #IFS=$SAVEIFS
    

       三.until循环

    until 循环执行一系列命令直至条件为 true 时停止。until 循环与 while 循环在处理方式上刚好相反

    until 循环格式为: 
    until command
    do
       Statement(s) to be executed until command is true
    done
    

     command 一般为条件表达式,如果返回值为 false,则继续执行循环体内的语句,否则跳出循环

    $ cat until01.sh 
    #!/bin/sh
    i=0
    until [ $i -gt 5 ]
    do
     let "i++"
     echo "this is $i"
    done
    

     一般while循环优于until循环,但在某些时候,也只是极少数情况下,until 循环更加有用。详细介绍until就不必要了


       四. break和continue命令

    1. break命令
    break命令允许跳出所有循环(终止执行后面的所有循环)
    2.continue命令
    continue命令与break命令类似,只有一点差别,它不会跳出所有循环,仅仅跳出当前循环。

    break01.sh
    #!/bin/sh
    for ((i=1;i<=5;i++))
    do
     if [ $i == 2 ];then
     break
     else
     echo "this is $i"
     fi
    done
    

     至于continue命令演示;你就把break替换下;执行看下效果就行了。不解释。

  • 相关阅读:
    CF 461B Appleman and Tree
    POJ 1821 Fence
    NOIP 2012 开车旅行
    CF 494B Obsessive String
    BZOJ2337 XOR和路径
    CF 24D Broken robot
    POJ 1952 BUY LOW, BUY LOWER
    SPOJ NAPTIME Naptime
    POJ 3585
    CF 453B Little Pony and Harmony Chest
  • 原文地址:https://www.cnblogs.com/lottu/p/4011743.html
Copyright © 2011-2022 走看看