0

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
l0b0
  • 55,365
  • 30
  • 138
  • 223
typeraj
  • 1
  • 1
  • 1
    `rm -f $APP` -> `rm -f "$APP"`. Use `pgrep` instead of `ps -fe | grep`. `[ "$VERSION" \< "$LATEST" ]` there is no such thing as `<` operator to [test command](https://linux.die.net/man/1/test). Use `-lt`. Please check you scripts at https://shellcheck.net – KamilCuk Mar 16 '20 at 01:02
  • 1
    Does this answer your question? [When to wrap quotes around a shell variable?](https://stackoverflow.com/questions/10067266/when-to-wrap-quotes-around-a-shell-variable) – KamilCuk Mar 16 '20 at 01:03
  • 2
    You're mixing `bash` and POSIX `sh` syntax, your shebang is `sh` and you have `[[ ]]` – Jetchisel Mar 16 '20 at 01:03
  • 1
    Try https://shellcheck.net to verify your script. – Jetchisel Mar 16 '20 at 01:05
  • You could open shell's debug mode with `bash -x test.sh`. Then paste the output here. – ahuigo Mar 16 '20 at 02:50
  • I guess you should use `rm -rf` instead of `rm -f` – ahuigo Mar 16 '20 at 02:56
  • Thanks everyone for catching the errors. I'm relatively new to this, so I kinda pieced bits of other scripts together to come up with this one. The double quotes around the `$APP` variable solved the delete issue, but also appreciate the tips on `pgrep` and shellchecker. – typeraj Mar 16 '20 at 09:42

1 Answers1

2

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. `

Digvijay S
  • 2,665
  • 1
  • 9
  • 21