in Bash, Tips & Tricks

BASH :: Always make sure to validate your inputs!

Got caught out not checking if a variable had been passed to a script that then used it to delete an .app.

Added in lines 8 – 11 to validate the input. Thankfully, only 3 machines affected!

#!/bin/bash

#  Get folder path when run through Casper (Comment out as applicable)
#  Enter the full path to the folder in the 'Parameter 4 Field' in Casper
# then check its not empty or it will delete everyhting from someones machine!
app=$4

if [[ $app == "" ]]; then
    echo "App field is empty. Stopping before everything is deleted!" >> /var/log/jamf.log
    exit 1
fi

FolderPath="/Applications/${app}"


# Check path exists then delete
if [[ -e "$FolderPath" ]]; then
  # delete
  /bin/rm -Rf "$FolderPath"
  if [[ ! -e "$Folderpath" ]]; then
    echo "${app} has been deleted." >> /var/log/jamf.log
  else
    echo "Removing ${app} failed." >> /var/log/jamf.log
  fi
fi

exit $?