Bash之break、continue和return命令在循环中的作用

简介:

1 continue:直接跳过本次循环,进入下一次循环。

#!/bin/bash

a=10

b=15

while [ $a -le $b ]

do

  ((a++))

  if [ $a -eq 11 ] || [ $a -eq 13 ]

    then

        continue

  fi

  echo $a

done


[root@master ~]# ./a.sh

12

14

15

16


2 break:此命令将会跳出循环

#!/bin/bash

a=8

b=15

while [ $a -le $b ]

do

  ((a++))

  if [ $a -gt 10 ]

    then

        break

  fi

  echo  $a

done


[root@master ~]# ./a.sh

9

10


3 return:return是返回的函数值并退出函数,通常0为正常退出,非0为非正常退出,把控制权交给调用函数。

exit:在程序运行的过程中随时结束程序,exit的参数是返回给系统的。

示例:

a=0

test () {

    if [ -f /root/b.sh ]

    then

        echo "This file is ok"

        return $a

    else

        echo "This is not exits"

        a=$?

    fi

}

test


[root@test ~]# bash -x b.sh

+ a=0

+ test

+ '[' -f /root/b.sh ']'

+ echo 'This file is ok'

This file is ok

+ return 0



本文转自 zengwj1949 51CTO博客,原文链接:http://blog.51cto.com/zengwj1949/1919414

相关文章
|
4月前
|
Linux Shell Windows
4:Bash shell命令-步入Linux的现代方法
4:Bash shell命令-步入Linux的现代方法
53 0
|
6月前
|
存储 Shell 索引
如何在Bash中使用For循环和数组?
如何在Bash中使用For循环和数组?
105 0
|
6月前
|
关系型数据库 MySQL Shell
【Linux命令】-bash: mysql: command not found
【Linux命令】-bash: mysql: command not found
56 0
|
4月前
|
Shell
-bash: lsb_release: 未找到命令
-bash: lsb_release: 未找到命令
|
4月前
|
Shell
在Shell(如Bash)中,`while`循环
在Shell(如Bash)中,`while`循环
41 2
|
5月前
|
Shell
bash 反弹命令的浅分析
bash 反弹命令的浅分析
28 0
|
8月前
|
Shell Linux
10.1.5 查询指令是否为 Bash shell 的内置命令: type
10.1.5 查询指令是否为 Bash shell 的内置命令: type
61 0
|
8月前
|
Shell 测试技术 Python
如何在Bash Shell脚本中使用`exec`命令?
如何在Bash Shell脚本中使用`exec`命令?
132 0
|
10月前
|
Java Shell Linux
如何在 Linux 中使用 Bash For 循环
如何在 Linux 中使用 Bash For 循环
90 0
|
10月前
|
Java Linux Shell
Centos7 bash:jps:未找到命令
Centos7 bash:jps:未找到命令
194 0