在macosx上重复curl + grep偶尔会给出不好的结果?

运行macosx 10.6.2,我看到一些非常奇怪的行为,用一个重复调用curl -o(file)的脚本,然后对其中的某个string进行grep。 偶尔grep返回1(找不到),当我期望0(find)。 这是脚本…

# do this 1000 times for ii in `cat count.txt`; do rm -f a.txt rm -f e.txt curl --fail --stderr e.txt -j -o a.txt -s $MYURL if [ -e a.txt ] ; then # Occasionally a.txt doesn't finish writing on time grep "login-url" a.txt >/dev/null LASTERR=$? echo $LASTERR is lasterr grep 1 if [ "$LASTERR" -ne "0" ] ; then cp a.txt anomaly.txt sleep 1 echo "Sleeping..." fi grep -q "login-url" a.txt >/dev/null LASTERR=$? echo $LASTERR is lasterr grep 2 if [ "$LASTERR" -ne "0" ] ; then echo "Dying..." exit 1 fi # This is what I actually want to do grep "login-url" a.txt >> out.txt fi done 

我所看到的是这样的:

 0 is lasterr 1 0 is lasterr 2 ... 0 is lasterr 1 0 is lasterr 2 0 is lasterr 1 1 is lasterr 2 

换句话说,两个grep之间的a.txt正在改变(据grep可以告诉)!

有其他人看过吗?

我注意到,如果我在curl调用之后放入“睡眠1”,问题就会消失。 那么,重复使用相同的文件名是否存在问题,或者是在写入之前返回curl,还是…?

这不是一个危机问题,因为“睡眠1”的解决方法,但我很紧张,因为我不明白的行为。

你可以尝试使两个grep命令匹配。 一个有-q ,另一个没有。 这可能不是问题,但值得消除分歧来缩小范围。

你的输出与脚本不匹配,所以这让我想知道你是否正在运行,看着两个不同的版本。 我自己做了很多次,造成了很大的困惑,直到我意识到发生了什么事情。

你有没有尝试bash的debugging输出?

 bash -x /path/to/script 

它会逐字输出每一步。