Shell script loop runs out of memory

Thrall, Bryan bryan.thrall@flightsafety.com
Thu May 31 19:05:00 GMT 2012


AZ 9901 wrote on 2012-05-31: 
> 2012/5/31 Jordan :
> Then, when (bash) scripting under Cygwin, you must take care to avoid
> forking as much as possible.
> 
> You could try to improve the "sleep 1" loop with the following one :
> 
> while md5sum $FILE_TO_CHECK | cut -d " " -f1 | grep -q "^$MD5PRINT$"
> do
>   sleep 1
> done
> 
> Note that MD5PRINTNEW is no more useful here.
> With this loop we avoid the fork done by
> MD5PRINTNEW=`md5sum $FILE_TO_CHECK | cut -d " " -f1`

Doesn't that just replace the 2 MD5PRINTNEW  forks (md5sum and cut) with 3 (md5sum, cut, and grep)?

Seems like the (untested) following would be better (in terms of fewer forks):

TMPFILE=$(mktemp)
md5sum $FILE_TO_CHECK > "$TMPFILE"
...
while md5sum -c "$TMPFILE"
do
  sleep 1
done
rm "$TMPFILE"

--
Bryan Thrall
Principal Software Engineer
FlightSafety International
bryan.thrall@flightsafety.com




More information about the Cygwin mailing list