First, you don't quote your variables. Second, -ne
is for comparing numbers, not strings. Use !=
instead:
if [ "$newsum" != "$checksum" ]
As Ed Heal pointed out in his comment, the algorithm only possibly makes sense if the file is being modified atomically. The best way to guarantee that is if it is generated in one place and then moved to kinect.tar
. Also, if the file is modified while the previous version is being built, your code will not catch the change, which can be fixed by the following change suggested by Ed Heal:
checksum=`md5sum kinect.tar`
while [ 1 ]
do
sleep 10
old_checksum=$checksum
checksum=`md5sum kinect.tar`
if [ "$old_checksum" != "$checksum" ]
Really, it would be best if you had a better way for the process writing the .tar
file to communicate with your script. For example, the new .tar
files are always fully written and then moved to kinect-new.tar
which the script checks for and moves to kinect.tar
. That avoids both the possibility of reading an incomplete file and the messiness of comparing md5sums. (You could be fancier and use something like inotify-tools to avoid having to explicitly poll, but it's possibly not worth the effort.)
Unfortunately, that does not solve the problem that the build may fail (unless the .tar
files you are using are known good releases, in which case it seems unlikely that you would want to check for a new one every 10 seconds). You probably want to make a directory, untar there, and only replace the old version if make
succeeds. Something along the lines of this:
tar -zxf kinect.tar
cd kinect
if make
then
cd ..
rm -rf kinect.good
mv kinect kinect.good
cd kinect.good
killall lebron
./lebron &
fi
cd .. # make sure we always stay in the original directory