3个值上的bash无限循环
我有3个静态值:3个值上的bash无限循环,bash,for-loop,infinite-loop,Bash,For Loop,Infinite Loop,我有3个静态值:aa,bb,cc。我想用退出案例无限期地循环它们。 编写简单循环很容易: aa-bb-cc中i的;做 回声$i 完成 但我想无限期地循环它们,直到某种情况出现: aa-bb-cc中i的;做 回声$i 如果[某个条件];然后 剂量 打破 fi 完成 somecondition取决于外部条件和i。它应该看起来像是尝试用i做一些事情,直到成功 最好的方法是什么?一种简单的方法是将代码嵌套在无限循环中: while true; do for i in aa bb cc; do
aa
,bb
,cc
。我想用退出案例无限期地循环它们。
编写简单循环很容易:
aa-bb-cc中i的;做
回声$i
完成
但我想无限期地循环它们,直到某种情况出现:
aa-bb-cc中i的;做
回声$i
如果[某个条件];然后
剂量
打破
fi
完成
somecondition
取决于外部条件和i
。它应该看起来像是尝试用i
做一些事情,直到成功
最好的方法是什么?一种简单的方法是将代码嵌套在无限循环中:
while true; do
for i in aa bb cc; do
echo $i;
if [ somecondition ]; then
doSomething
break 2
fi
done
done
请注意,break
命令现在有一个参数2
manbash
说:
break [n]
Exit from within a for, while, until, or select
loop. If n is specified, break n levels. n
must be >= 1. If n is greater than the number
of enclosing loops, all enclosing loops are
exited. The return value is 0 unless n is not
greater than or equal to 1.