开发者

Bash conditional statement doesn't work under Cygwin

I have a Makefile rule that generates an MD5 hash for a file, compares it with stored hash from the previous run, and if it is different, updates the stored hash. (I have some files that are generated from a database, so their timestamps are always new and without a MD5 hash I wouldn't know if they really changed or not.) Here it is:

CURR=`$(MD5) -q $<`; \
PREV=`if [ -e $@ ] ; then cat $@ ; fi` ; \
if [ "$$CURR" != "$$PREV" ]; then echo $$CURR > $@ ; fi

(The $(MD5) here is the md5 tool, which is md5 on Mac and md5deep on Cygwin.)

For some reason this works fine in bash under 开发者_JS百科Mac OS X, but doesn't work under Cygwin. I.e. it executes, but the conditional always evaluates to true, so it always updates the hash. I added echo $$CURR $$PREV and I see that the hashes look identical, but nonetheless it always updates the hash file.

What am I missing?


echo "'$$CURR' '$$PREV'" and check the whitespace. my guess is that you'll find a difference when using the single quotes (the surrounding double-quotes are necessary for the variables to be interpolated).

if that's the case, one way to fix it is to make sure PREV always contains something, say with else echo X, then remove the double-quotes: if [ $$CURR != $$PREV ];

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜