-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'切换到'=' - 因此,使基于字符串的比较而不是数字 - 实际上会使您更加坚固,以对抗我怀疑的故障模式)。 –