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