UNIX cache problem?

So I worked out a script for Mac OSX from an existing script. This script checks the versionnumber of a plugin and a package. If the plugin version is different from the package it has to update the plugin. The script works fine but the final check fails.

Here is my script:

#compare version numbers
installedversion=`/usr/bin/defaults read /location/plugin.plugin/Contents/Info CFBundleShortVersionString`
newestversion=`/usr/bin/defaults read /otherlocation/plugin.pkg/Contents/Info CFBundleShortVersionString`
if [ "${installedversion}" != "${newestversion}" ]; then
installer -pkg /otherlocation/plugin.pkg -target /
# check if the installation succeeded
newlyinstalledversion=`/usr/bin/defaults read /location/plugin.plugin/Contents/Info CFBundleShortVersionString`
    if [ "${newestversion}" = "${newlyinstalledversion}" ]; then
        /bin/echo "`date`: SUCCESS: ${newlyinstalledversion} installed..." >> ${logfile}
        else
        /bin/echo "`date`: ERROR: update unsuccessful, version remains at {installedversion}." >> ${logfile}
    fi
fi

So lets say the installedversion is OLD and the newestversion is NEW.
It keeps saying ERROR. If I manually check the version number it's updated to version NEW.
If I add a line to the script to echo the ${newlyinstalledversion} to the log file it says OLD.
I tried to add 10 or 20 seconds 'sleep' after the installer command but that didn't help.
Is this a cache problem?

Is this OS X?

Sorry, forgot to mention. Yes, it's for Mac OS X 10.8.

In older versions of OS X the filesystem would sync every 30 seconds by a daemon called updated. It's gone in the latter versions with the advent of launchd. My guess is FSEvents is now involved.

Well I'm pretty new to unix scripting so FSEvents doesn't directly ring a bell. I've searched it and I think I understand what is does. But I don't really see the connection to my problem.
Is it maybe because I define two variables with the same path?

     installedversion=`/usr/bin/defaults read /location/plugin.plugin/Contents/Info CFBundleShortVersionString`
newlyinstalledversion=`/usr/bin/defaults read /location/plugin.plugin/Contents/Info CFBundleShortVersionString`

Set your script in debug mode by adding set -x on the next line after the shebang. This will display the value of your variables.

Thanks, that comes in handy! But the results only show what I already knew. But I got a little further. I thought maybe it just needs more time. So I put 40 seconds after the installer command and now it did say version NEW. Then I changed it to 20 seconds and again version NEW. But when I changed it to 10 seconds it said version OLD.
The strange thing is that when I look at the actual .plugin in Finder during the installation, the version almost directly changes to NEW. After that the sleep command runs for 10 seconds. After the ten seconds it still reads out version OLD.

So somehow it does cache the information somewhere...

---------- Post updated 03-01-13 at 11:02 AM ---------- Previous update was 02-01-13 at 02:34 PM ----------

Oke,

So I found another workaround, not a real solution. The plugin has besides an Info.plist also a Version.plist. So I've done some testing.

If I use the CFBundleShortVersionString from the Version.plist for the installedversion as well as the newlyinstalledversion I get the same problem. But If I use the Version.plist for the installedversion and the Info.plist for the newlyinstalledversion then it works properly. Even without the 10 seconds sleep.

Since this script can be used for more software other than this plugin this is not a real solution. Because not all plugins or apps have the version.plist.

I also tried unset installedversion but that didn't help either...

---------- Post updated at 11:54 AM ---------- Previous update was at 11:02 AM ----------

I also found something in another post that maybe will help finding a solution: