Succinct version of `if..elif`

Instead of writing out a large block for an if / elif such as…

if [[ $foo == $bar ]]; then
    echo "yes"
else
    echo "no
fi

a much shorter version can be used instead:

[[ $foo == $bar ]] && echo "yes" || echo "no"

A simple [[ condition ]] && positive result || negative result

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 $?

Bash :: Check PKG Receipt Exists

While scripting a configuration manifest for DEPNotify, it would skip to the next policy before the last had completed, and so would skip to the end and close the splash screen before the build was finished.

This defeats the purpose of having a screen stopping any interaction until the machine had completed so after a little interaction on the #DEPNotify slack channel on Macadmins, it was suggested to write a bom file as the last action of a policy but this would require every policy to have additional work to add this.

JAMF already writes an empty PKG or DMG to `/Library/Application Support/JAMF/Receipts/` upon completion so why not use this?

Below is the function I wrote:

#!/bin/bash

# Check to see if package receipt exists. Time range is 1 mins
# if it does, it returns the full package name. If not, it waits until
fileExists() {
  # Package name passed with FUNC
  package=$1
  
  # Path to JAMF pkg receipts
  receiptPath="/Library/Application Support/JAMF/Receipts"
  
  # Time range for find: currently 30 mins
  range="$(date -v -30M)"
  
  # Find pkg receipts that match $1 less than 30 mins old
  results="$(find "${receiptPath}" -newermt "$range" | grep -i $package)"
  
  # Strip the path
  results="$(basename "$results")"

  # Loop until result exists; echo pkg name when it does
  while :; do
    [[ $results != "" ]] && echo $results && break
    sleep 1
  done
}