zoukankan      html  css  js  c++  java
  • break continue exit return 的区别

    [root@localhost day1]# cat ss.sh 

    #!/bin/bash


    for ((i=0;i<5;i++))

    do
    if [ $i -eq 3 ]
    then
    break
    #continue
    #exit

    #return

    fii
    echo $i;sleep 1
    done
    echo "ok"

    ###################################################

    [root@localhost day1]# sh ss.sh           #由此看出break是直接终断循环的
    0
    1
    2
    ok

    ——————————————————————————————

    [root@localhost day1]# sh ss.sh           #continue效果 还是中断当前循环,继续进行下一循环
    0
    1
    2
    4
    ok

    ——————————————————————————————

    [root@localhost day1]# sh ss.sh           #exit 直接退出了脚本
    0
    1
    2

    return :return must be function 

    1.中断循环

    2.返回值

    [root@localhost day1]# sh return.sh
    0
    1
    2
    return.sh: line 12: return: can only `return' from a function or sourced script    #必须是函数
    3
    4
    ok

    #!/bin/bash

    fuction_return (){              #加入函数
    for ((i=0;i<5;i++))

    do
    if [ $i -eq 3 ]
    then
    #break
    #continue
    #exit
    return
    fi
    echo $i;sleep 1
    done
    echo "ok"
    }
    fuction_return

    [root@localhost day1]# sh return.sh               #结果
    0
    1
    2

  • 相关阅读:
    分组排序并显示序号
    power-design--tables-export-usage
    cache implement
    get system properties
    jbpm
    JVM内存管理机制和垃圾回收机制
    java读取excel
    Java编程中“为了性能”尽量要做到的一些地方
    json串与java对象互转
    apidoc的使用
  • 原文地址:https://www.cnblogs.com/sxgaofeng/p/9174413.html
Copyright © 2011-2022 走看看