1
如何在shell腳本中跳出無限循環while
?循環首次運行在shell腳本中繼續
我想要實現在shell腳本(S)以下PHP
代碼:
$i=1;
while(1) {
if ($i == 1) continue;
if ($i > 9) break;
$i++;
}
如何在shell腳本中跳出無限循環while
?循環首次運行在shell腳本中繼續
我想要實現在shell腳本(S)以下PHP
代碼:
$i=1;
while(1) {
if ($i == 1) continue;
if ($i > 9) break;
$i++;
}
i=1
while [ $i -gt 9 ] ; do
# do something here
i=$(($i+1))
done
是你能做到這一點的方法之一。
HTH
break
作品在shell腳本爲好,但最好要檢查的條件while
子句中比在循環內,如索爾特建議。假設你有在循環一些更復雜的邏輯檢查條件(也就是你真正想要的是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,並一直持續到下一次迭代,如果結果爲真,增加$我之前,你實際上哈瓦無限循環。 –
你的php代碼中有一個無限循環。 – favoretti