-1
#!/bin/sh
count=0
foo=0
echo "foo is $foo"
while [ "$foo" -eq 0 ] && [ "$count" -lt 10 ]; do
echo "inside while: foo is $foo"
count=$((count+1))
foo=1
done
echo "after while"
你會期望上面的腳本輸出以下內容,對吧?我的殼破了嗎?
foo is 0
inside while: foo is 0
after while
而且它在很多其他機器上都有,就像你自己的機器一樣。但不是我的...
foo is 0
inside while: foo is 0
inside while: foo is 1
inside while: foo is 1
... (infinite loop)
我做錯了什麼,或者我錯過了明顯的東西?
在這個(壞掉的)機器上,如果我調整while
有條件使用「過時」-a
操作符,它會「修復」問題(不管它是什麼)。爲什麼?
要調查的第一件事是什麼樣的shell和版本是你的/ bin/sh? - 然後調查在該shell啓動時運行的任何啓動腳本。 – nos
......這就是說 - 正如我在你的其他問題中所建議的那樣,你會考慮使用'set -x'來記錄運行時的單個命令嗎?實際上,我可以考慮在腳本中隱藏字符的可能方式可能會導致此行爲,並且使用'set -x'可以清楚地表明是否是這種情況。 –
(從'-eq'切換到'=' - 因此,使基於字符串的比較而不是數字 - 實際上會使您更加堅固,以對抗我懷疑的故障模式)。 –