Skip to content

Instantly share code, notes, and snippets.

@triti
Last active August 30, 2016 19:54
Show Gist options
  • Save triti/595c89076a8db738b8da to your computer and use it in GitHub Desktop.
Save triti/595c89076a8db738b8da to your computer and use it in GitHub Desktop.
Install all Apple software updates in a DeployStudio postinstall script. I wrote my own script because of a bug with the Software Update plugin in DeployStudio 1.6.12.
#!/bin/bash
# https://gist.github.com/595c89076a8db738b8da
# DeployStudio postinstall script to install all available Apple software
# updates. I wrote this because of a bug with the Software Update plugin
# in DeployStudio 1.6.12 that causes an infinite loop in certain circumstances.
installed_updates=()
# Helper function since Bash 3 doesn't have associative arrays.
# Probably means I should be doing this in python.
function is_update_installed () {
for installed in "${installed_updates[@]}"; do
if [[ "$1" = "${installed}" ]]; then
return 0
fi
done
return 1
}
while true; do
available_updates=()
restart_required=false
printf 'Checking if updates are available...\n'
# Loop over the list of software updates.
sus_output="$(/usr/sbin/softwareupdate --list 2>/dev/null)"
while IFS= read -r line; do
# If the line begins with three spaces and an asterisk then it is a
# required update.
if [[ "${line}" = ' *'* ]]; then
name="${line:5}"
if is_update_installed "${name}"; then
printf 'Update "%s" has already been installed. Skipping.\n' "${name}"
else
printf 'Adding "%s" as an available update.\n' "${name}"
available_updates+=("${name}")
fi
# If the line ends with the [restart] tag then a reboot will be required.
elif [[ "${line}" = *'[restart]' ]]; then
printf 'Update requires a restart.\n'
restart_required=true
fi
done <<< "${sus_output}"
printf '\nTo Be Installed:\n'
printf '\t"%s"\n' "${available_updates[@]}"
printf 'Available Updates: %s\nRestart Required: %s\n\n' ${#available_updates[@]} ${restart_required}
# Install any available updates.
if [[ ${#available_updates[@]} -gt 0 ]]; then
printf 'Installing all available updates...\n'
/usr/sbin/softwareupdate --install "${available_updates[@]}"
if [[ ${restart_required} = true ]]; then
printf 'Restart required. Exiting.\n'
printf 'Ignore the warning that DeployStudio is about to give. This script is not failing.\n'
exit 100
fi
else
# Exit when we run out of updates to install.
printf 'No available updates remaining. Exiting.\n'
exit 0
fi
printf 'Remembering installed updates.\n\n'
for update in "${available_updates[@]}"; do
installed_updates+=("${update}")
done
done
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment