【发布时间】:2019-11-20 03:58:35
【问题描述】:
我想打破嵌套的 while 循环。
以下是我在代码中尝试过的,
while [itretative condition]
do
...
...
cat test1.json | while read line
do
...
...
cat test2.json | while read line
do
...
...
if [ "$taskstatus" = "RUNNING" ]
#when my task status reach running, i want to stop the script execution and end it.
break 3 #break 3 or exit is not working for me.
fi
done
done
done
请建议我如何实现这一目标?
【问题讨论】:
-
你的缩进太疯狂了,当边距随机跳跃时,真的很难理解应该发生的事情。另外,您能否解释一下您的期望,以及输出的错误之处?
-
对不起我的缩进,我的期望是!当任务状态达到“RUNNING”状态时..我希望脚本优雅地结束,我不想进一步循环并测试但结束它。
-
如果这是你的整个脚本,那么
exit会很轻松地做到这一点。如果您无法解决问题,我认为您无法让任何人看到它。另请参阅创建minimal reproducible example 的指南。 -
嗨,我尝试退出,但对我来说没有成功。根据您的建议,我通过删除所有不必要的信息来表示代码。
-
管道 (
cat somefile | while ...) 在子 shell 中运行它们的内容,并且子 shell 不能中断父 shell 中的循环(或退出父 shell,或者...)。你能用while read ... done <somefile替换cat管道吗?
标签: bash shell loops nested-loops break