zoukankan      html  css  js  c++  java
  • shell脚本--循环结构

      shell的循环结构有while和for两种

    for循环

    #!/bin/bash
    #文件名:test.sh
    
    i=4
    
    for i in 2 4 6 8 10
    do
        echo $i
    done
    
    echo $i

      运行:

    ubuntu@ubuntu:~$ ./test.sh
    2
    4
    6
    8
    10
    10
    ubuntu@ubuntu:~$ 
    

      注意最后一次输出是10,而不是其他语言中输出4,这一点和JavaScript很类似。

      for循环要循环的内容就是跟在in后面,然后以空格分隔,可能要循环的内容特别多,那么就可以将要循环的内容保存在一个文件中,然后读出来即可。比如下面:

    #!/bin/bash
    #文件名:test.sh
    
    for i in `cat test.txt`
    do
        echo $i
    done
        echo $i
    

      往test.txt中添加一点内容:

    ubuntu@ubuntu:~$ echo 1 3 5 7 9 > test.txt
    ubuntu@ubuntu:~$ ./test.sh
    1
    3
    5
    7
    9
    9
    ubuntu@ubuntu:~$ 
    

      shell默认会将空格、制表符( )、换行看做是分隔符,所以:

      1、无论内容是以换行或者制表符分隔,for循环都会将其看作是空格分隔,效果上是一样的。

      同时要注意,如果for循环中,集合中的某一项中间包含空格,那么请使用引号将其括起来,因为for循环是以空格分隔的。

      前面讲for循环和其他语言的for循环形式上的差别很大,但是呢,其实shell中for循环也可以向其他语言那样是用for,例子如下:

    #!/bin/bash
    #文件名:test.sh
    
    for ((i=0;i<4;i++))
    do
        echo $i
    done
    

      运行:

    ubuntu@ubuntu:~$ ./test.sh
    0
    1
    2
    3
    ubuntu@ubuntu:~$ 
    

      一定要注意着一种for循环中,使用的是两对括号,在括号中使用像其他语言的三个表达式的格式。其实着一种for循环的do和done可以换成{  和  } 。仅限于for循环,其他循环和判断结构不适用。

    自定义分隔符

      上面已经说了,shell默认是以空格、制表符、换行进行分隔,那么如果需要强制分隔符是什么时,可以修改IFS这个环境变量。

    #!/bin/bash
    #文件名:test.sh
    
    IFS='x'  #以x为分隔
    #IFS=':' #以:为分隔
    #IFS=$'
    '  #如果是特殊字符,需要在前面加上$
    v="aaaaxbbbbbxcccccxddddd"
    #其实$v此时的内容是aaaa bbbbb ccccc dddd
    for s in $v
    do
        echo $s
    done

      运行:

    ubuntu@ubuntu:~$ ./test.sh
    aaaa
    bbbbb
    ccccc
    ddddd
    

      

      

    while循环

      while后面跟着判断条件,判断条件可以为多个,但是每一个判断条件都要是用单独的 [  ]括起来,然后多个判断之间使用 &&、||来表示含义。

      例子:

    #!/bin/bash
    #文件名:test.sh
    
    tot=0
    num=10
    while [ $num -gt 0 ]
    do
        tot=$[ $tot + $num ]
        num=$[ $num - 1 ]
    done
    
    echo "总和为"$tot

      运行结果如下:

    ubuntu@ubuntu:~$ ./test.sh
    总和为55
    ubuntu@ubuntu:~$ 
    

      

    until循环

      until循环和while循环刚好相反

      对于while,如果条件成立,则进入循环,否则条件不成立时,就结束循环。

      until则是相反的,如果条件不成立,则进入循环;如果条件成立,则结束循环。

      用until改写上面的例子:

    #!/bin/bash
    #文件名:test.sh
    
    tot=0
    num=10
    #num小于或等于0时,结束循环,否则进入循环
    until [ $num -le 0 ]
    do
        tot=$[ $tot + $num ]
        num=$[ $num - 1 ]
    done
    
    echo "总和为"$tot
    

      运行测试结果和上面一样。

  • 相关阅读:
    最大子数组求和并进行条件组合覆盖测试
    Ubuntu 16.04 c++ Google框架单元测试
    The directory '/home/stone/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If execu
    Problem executing scripts APT::Update::Post-Invoke-Success 'if /usr/bin/test -w /var/cache/app-info -a -e /usr/bin/appstreamcli; then appstreamcli refresh > /dev/null; fi'
    个人博客作业三:微软小娜APP的案例分析
    补交 作业一
    补交 作业二:个人博客作业内容:需求分析
    嵌入式软件设计第12次实验报告
    嵌入式软件设计第11次实验报告
    嵌入式软件设计第10次实验报告
  • 原文地址:https://www.cnblogs.com/-beyond/p/8279059.html
Copyright © 2011-2022 走看看