typeraj
typeraj

Reputation: 1

Trying to use 'rm' in a script

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

Upvotes: 0

Views: 1343

Answers (1)

Digvijay S
Digvijay S

Reputation: 2705

APP=/Applications/8x8\ -\ Virtual\ Office.app

Your path contains spaces. So remove command should be rm -f "$APP"

Demo:

:=>APP=/Applications/8x8\ -\ Virtual\ Office.app
:=>echo $APP
/Applications/8x8 - Virtual Office.app
:=>rm $APP
rm: cannot remove '/Applications/8x8': No such file or directory
rm: cannot remove '-': No such file or directory
rm: cannot remove 'Virtual': No such file or directory
rm: cannot remove 'Office.app': No such file or directory
:=>rm "$APP" 
rm: cannot remove '/Applications/8x8 - Virtual Office.app': No such file or directory
:=>

In the above demo when we execute rm $APP rm command is taking 4 arguments Applications/8x8 , - , Virtual ,Office.app becasue of spaces in path. `

Upvotes: 2

Related Questions