一篇博客分清shell中的狀態返回值-return-break-continue-exit

一篇博客分清shell中的狀態返回值-return-break-continue-exitshell

 

1、breakcontinueexitreturn的區別和對比vim

 

條件與循環控制及程序返回值命令知識表bash

命令ide

說明函數

break noop

若是省略n,則表示跳出整個循環n表示跳出循環的層數測試

continue nui

若是省略n,則表示跳出本次循環,忽略本次循環剩餘代碼,進入循環的下一次循環。n表示退到第n層繼續循環spa

exit norm

表示退出當前shell程序n爲上一次程序執行的狀態返回值,n也能夠省略,在下一個shell裏能夠經過「$?」接收exit nn值。

return n

用於在函數裏做爲函數的返回值,以判斷函數執行是否正確,在下一個shell裏可經過「$?」接收exit nn

 

 

 

2、breakcontinueexit功能執行流程圖

 

1、在循環中break功能的執行流程邏輯圖


wKioL1mAZC7BCACTAABfUCx4vyc192.png

wKiom1mAZC6iNRXEAABepFOOimA055.png


 

 

 

 

2、在循環中bcontinue功能的執行流程邏輯圖




wKioL1mAZEPQQVeoAABfsEAdVjk688.png

 wKiom1mAZUaDzA7TAABhWoNhKds129.png



三、在循環中exit功能的執行流程邏輯圖

wKioL1mAZFLT3LpqAABptYgH8TE585.png

wKiom1mAZFLRQ_DtAABqMx-WLA8644.png


 

3、用一個小腳本區分breakcontinueexitreturn

[root@xuegod63 ~]# vim 3.sh 
#!/bin/bash
#
#User : Mobanche
#Date : 2017-8-1
#Description :This shell script is used primarily to identify the state 
#             return value of a loop control that is distinguished from 
#             the return-break-continue-exit
 
if [ $# -ne 1 ]
then
        echo "usage: {conntiue|break|exit|return}"      
        exit 1
fi
 
test () {
        for ((i=1;i<=5;i++))
        do
                if [ $i -eq 3 ]
                then
                        $*
                fi
                echo $i
        done
        echo "I am function"
}
test $*
func_ret=$?
if [ `echo $*|grep return|wc -l` -eq 1 ]
then
        echo "return's exit status :$func_ret"
fi
echo OK


 

 

測試:

wKioL1mAZGDxWj05AAApnDV2_Pg131.png

相關文章
相關標籤/搜索