从嵌套的 for 循环中跳出
Break from imbricated for loop
我想知道有没有办法让循环层叠起来:
check_mac_address() {
local mac=""
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
}
感谢任何帮助
break
接受一个参数,该参数指定要打破多少层周围循环;在你的情况下,我相信它会是 3:
http://www.gnu.org/software/bash/manual/bashref.html#Bourne-Shell-Builtins
来自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.
所以在你的情况下,你可以打破所有三个循环
break 3
我想知道有没有办法让循环层叠起来:
check_mac_address() {
local mac=""
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
}
感谢任何帮助
break
接受一个参数,该参数指定要打破多少层周围循环;在你的情况下,我相信它会是 3:
http://www.gnu.org/software/bash/manual/bashref.html#Bourne-Shell-Builtins
来自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.
所以在你的情况下,你可以打破所有三个循环
break 3