如何在shell脚本中跳出无限的while循环?
我想在shell脚本中实现以下PHP代码:
$i=1;
while( 1 ) {
if ( $i == 1 ) continue;
if ( $i > 9 ) break;
$i++;
}发布于 2011-11-30 17:57:17
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发布于 2011-11-30 17:47:41
i=1
while [ $i -gt 9 ] ; do
# do something here
i=$(($i+1))
done是你可以做到的一种方式。
HTH
https://stackoverflow.com/questions/8323815
复制相似问题