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

  • 相关阅读:
    html中滚动条的样式
    在个人机上发布web项目
    Apache与SVN的集成
    待完成
    chmod
    【转】ubuntu修改IP地址和网关的方法
    ubuntu 添加svn服务
    生成指定大小的空文件
    数码单反相机完全攻略
    【转】ubuntu subversion安装
  • 原文地址:https://www.cnblogs.com/sxgaofeng/p/9174413.html
Copyright © 2011-2022 走看看