如何打破while
shell脚本中的无限循环?
我想PHP
在 shell 脚本中实现以下代码:
$i=1;
while( 1 ) {
if ( $i == 1 ) continue;
if ( $i > 9 ) break;
$i++;
}
break
也可以在 shell 脚本中工作,但while
正如 Zsolt 建议的那样,检查子句中的条件比检查循环内的条件更好。假设您在检查条件之前在循环中有一些更复杂的逻辑(也就是说,您真正想要的是一个do..while
循环),您可以执行以下操作:
i=1
while true
do
if [ "$i" -eq 1 ]
then
continue
fi
# Other stuff which might even modify $i
if [ $i -gt 9 ]
then
let i+=1
break
fi
done
如果你真的只是想重复一些事情$count
,有一个更简单的方法:
for index in $(seq 1 $count)
do
# Stuff
done
i=1
while [ $i -gt 9 ] ; do
# do something here
i=$(($i+1))
done
是您可以做到的方法之一。
高温高压