I wrote this bash script to remove the old version of a Mac application and install the new version. It all works well, except it doesn't seem to delete the application before installing the new one - instead, it seems to write over the top of it, which is causing some issues when you try to launch the application. Wondering where I'm going wrong in my script - any help would be much appreciated.
#!/bin/sh
#Script to remove the old version of 8x8 Virtual Office and the install the latest version.
APP=/Applications/8x8\ -\ Virtual\ Office.app
VERSION=$(defaults read /Applications/8x8\ -\ Virtual\ Office.app/Contents/Info | grep CFBundleShortVersionString | cut -c35-39)
#See Script options in JSS for $4 value
LATEST="$4"
pid=$(ps -fe | grep '8x8 - Virtual Office' | grep -v grep | awk '{print $2}')
if test -e "$APP" ; then
  if [ "$VERSION" \< "$LATEST" ] ; then
    echo "8x8 Virtual Office $VERSION found"
    echo "Application needs updating..."
    if [[ -n $pid ]]; then
      echo "Quitting application first..."
      kill $pid
      sleep 5s
      echo "Removing old version..."
      rm -f $APP
      sleep 5s 
      echo "Installing new version..."
      jamf policy -event install88
    else
      echo "Application not running, removing old version..."
      rm -f $APP
      sleep 5s 
      echo "Installing new version..."
      jamf policy -event install88
      exit 0
    fi
  else
    echo "No update required"
    exit 0
  fi
else
  echo "8x8 Virtual Office not found, installing..."
  jamf policy -event install88
    exit 0
fi
 
     
    