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

  • 相关阅读:
    python3+request接口自动化框架
    类型转换函数
    操作符重载(三)
    操作符重载(二)
    操作符重载(一)
    时间获取函数
    文件和目录
    Linux五种IO模型
    类中的函数重载
    系统调用IO和标准IO
  • 原文地址:https://www.cnblogs.com/sxgaofeng/p/9174413.html
Copyright © 2011-2022 走看看