stack_A
stack_A

Reputation: 763

Break from imbricated for loop

I want to know if there is a way to leave imbricated for loop:

check_mac_address() {
    local mac="$1"
    for wunit in `get_wunit`; do
        for iuc in `get_iuc`; do            
            for assoc_mac in `get_iuc $wunit $iuc`;do
                if [ "$assoc_mac" = "$mac"]; then 
                    local int_type="WF" 
                    break #---> break from all loop  
                else
                    int_type="ETH"
                    break #---> break from all loop  
                fi 
            done
        done
    done
}

any help is appreciated

Upvotes: 2

Views: 273

Answers (2)

user000001
user000001

Reputation: 33387

From http://tldp.org/LDP/abs/html/loopcontrol.html

A plain break terminates only the innermost loop in which it is embedded, but a break N breaks out of N levels of loop.

So in your case you to break from all three loops you can do

break 3

Upvotes: 2

Peter - Reinstate Monica
Peter - Reinstate Monica

Reputation: 16049

break takes a parameter which specifies how many levels of surrounding loops to break; in your case I believe it would be 3:

http://www.gnu.org/software/bash/manual/bashref.html#Bourne-Shell-Builtins

Upvotes: 4

Related Questions