bash while Break 跳出循环而不是循环?
任何人都知道一种简单的方法可以立即退出 bash 中 while/until 循环的当前迭代,但继续循环中的下一个迭代?
我的基本结构是:
sort -u -r -g -k 5 -t ' ' $filteredtoptrigsf |\
while read -a linA; do
stuff
morestuff
[[ $smthStinks -eq 1 ]] && break
[[ ${linA[*]} == $onlyThisLineStinks ]] && done
lotsaSlowProcessing
doSmthDangerous
done
在第一个 [[ ... ]] 测试中,由于某些计算条件,我肯定想跳出 while 循环并且永远不会回来。
在第二个 [[ ]] 测试中,满足一个条件,该条件只能跳出循环一次,避免在其下面进一步执行代码,但继续读取下一行输入并让 while 继续。
正如你所看到的,我在那里尝试了 done
但只有令牌意外的错误。
我不想将循环的大部分代码包装在 if 和 else 中,因为实际上有相当多的终止条件,无论是全局的还是仅针对当前循环。
Anyone know of a simple way to immediately exit the current iteration of a while/until loop in bash, but continue to the next iteration in the loop?
My basic structure is:
sort -u -r -g -k 5 -t ' ' $filteredtoptrigsf |\
while read -a linA; do
stuff
morestuff
[[ $smthStinks -eq 1 ]] && break
[[ ${linA[*]} == $onlyThisLineStinks ]] && done
lotsaSlowProcessing
doSmthDangerous
done
In the first [[ ... ]] test I definitely want break out of the while loop and never come back, due to some calculated conditions.
In the second [[ ]] test a condition is met that should only break out of the cycle once, avoiding further code below it, but continue reading the next line of input and let while continue.
As you can see I tried done
there but that only errors with token unexpected.
I would rather not wrap much of the loop's code inside if's and else's since there are actually quite of few terminating conditions, both global and for current cycle only.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试
继续
而不是完成
。这将带您进入循环的下一次迭代。Try
continue
instead ofdone
. That takes you to the next iteration of the loop.