-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathMac-Software-Updates.sh
More file actions
98 lines (72 loc) · 2.87 KB
/
Mac-Software-Updates.sh
File metadata and controls
98 lines (72 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/bin/bash
# This script attempts to get a list of updates available from Apple via softwareupdate.
# The list is parsed and stripped of any updates that require a reboot, then those remaining
# labels are passed along to the softwareupdate command (if the optional parameter passed
# to the script is "true")
# This script is provided AS IS without warranty of any kind.
# https://github.com/Mac-Nerd/Mac-scripts
# -----------------------------------------------------------
# Check your privilege
if [ "$(whoami)" != "root" ]; then
echo "This script must be run with root/sudo privileges."
exit 1
fi
ListOfSoftwareUpdates="/tmp/ListOfSoftwareUpdates"
# Function definition:
checkForSoftwareUpdates(){
echo "Refreshing available updates."
# Restart the softwareupdate daemon to ensure latest updates are being picked up
/bin/launchctl kickstart -k system/com.apple.softwareupdated
# Allow a few seconds for daemon to startup
/bin/sleep 3
# Store list of software updates in /tmp which gets cleared periodically by the OS and on restarts
/usr/sbin/softwareupdate -l > "$ListOfSoftwareUpdates" 2>&1
}
# is the list already there?
if [ -f $ListOfSoftwareUpdates ]
then
# echo "$ListOfSoftwareUpdates exists."
# if it's there, how old is it?
ListAge=$((($(date +%s) - $(date -r "$ListOfSoftwareUpdates" +%s))/60 ))
# echo "$ListAge minutes old."
if [ $ListAge -ge 60 ]
then
# older than an hour
echo "Older than 1 hour, checking for updates again."
checkForSoftwareUpdates
fi
else
# doesn't exist
echo "$ListOfSoftwareUpdates does not exist. Checking for updates."
checkForSoftwareUpdates
fi
# check the list for updates requiring restart
RestartRequired=$(grep -B1 -i restart "$ListOfSoftwareUpdates") # updates requiring restart
echo "NOTICE: There are updates that require a restart. These will not be installed:"
printf "%s\n\n" "$RestartRequired"
# if not empty, then strip out "Restart required" leaving only "Recommended" updates. Otherwise, just get the list of updates that are "recommended"
if [[ $RestartRequired ]]
then
AllUpdates=$(grep -B1 -i recommended "$ListOfSoftwareUpdates" | grep -v -F "$RestartRequired")
else
AllUpdates=$(grep -B1 -i recommended "$ListOfSoftwareUpdates")
fi
if [ -n "$AllUpdates" ]
then
# finally, remove the information lines and the "label" text, leaving only the list of update packages to install
UpdatesNoRestart=$(echo "$AllUpdates" | grep -v -e "--" | grep -B1 -i recommended | grep -i "Label" | cut -d : -f 2 )
# send this to softwareupdate -i
# trims whitespace
UpdatesNoRestart=$(echo "$UpdatesNoRestart" | xargs)
# needs quotes, since some updates have spaces in name.
echo "The following updates are available to install:"
echo "$UpdatesNoRestart"
else
echo "No updates available."
exit 0
fi
if [[ $1 =~ ["true"|"True"] ]]
then
echo "Installing updates."
/usr/sbin/softwareupdate -i --verbose "$UpdatesNoRestart"
fi