Skip to content

Instantly share code, notes, and snippets.

@apizz
Created March 13, 2018 23:06
Show Gist options
  • Save apizz/a6b47878ac3d18e2624e959fd722d58c to your computer and use it in GitHub Desktop.
Save apizz/a6b47878ac3d18e2624e959fd722d58c to your computer and use it in GitHub Desktop.
Download Apple Security Update
#!/bin/bash
# Must match the update name exactly as it appears in the `softwareupdate -l` listing
PATCH_TO_MATCH="$4"
# Security Product ID number - number of the folder in /Library/Updates with contained updates
SEC_PROD_KEY="$5"
# Defined minimum allowed free space % in order to download the update
MINPERCENTFREE="$6"
# Available space of boot volume /
AVAILSPACE=$(/bin/df / | /usr/bin/tail -1 | /usr/bin/awk '{print $4}')
# Total storage capacity of boot volume /
DRIVESTORAGE=$(/bin/df / | /usr/bin/tail -1 | /usr/bin/awk '{print $2}')
# Percentage of available storage space
PERCENTFREE=$(/bin/echo $(( $AVAILSPACE * 100 / $DRIVESTORAGE )))
# If your defined minimum free space % is less than what's available, prevent download and exit
if [ "$PERCENTFREE" -lt "$MINPERCENTFREE" ]; then
/bin/echo "Insufficient free space available. Exiting script."
exit 1
fi
# Searches Apple list of available updates for the first available security update
SEC_UPDATE=$(sudo /usr/sbin/softwareupdate -l | /usr/bin/grep Security | /usr/bin/head -1 | /usr/bin/sed 's/^.*Security/Security/')
# If security update name from Apple list matches expected name, check that
# it isn't already downloaded, and then download security update
if [ "$PATCH_TO_MATCH" = "$SEC_UPDATE" ]; then
/bin/echo "Downloading ${SEC_UPDATE} ..."
/usr/sbin/softwareupdate -d "$SEC_UPDATE"
exitcode=$(/bin/echo $?)
else
# In the event the expected security update is not found in the softwareupdate list
/bin/echo "ERROR - No Security Update Match Found"
exit 1
fi
# Check download completed successfully
if [ "$exitcode" = 0 ]; then
/bin/echo "${SEC_UPDATE} Download Completed Successfully!"
fi
exit $exitcode
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment