Help with Apple software update script

May
Contributor III

Hi all,
i'm working on a script that installs only the updates listed in the script parameters $4 to $11

i've got it all working nicely for just one update, it pulls softwareupdate -l, egrep only the updates listed in $4 to $11, then formats the result into an array (with just has the description and no spaces except between each update then installs as below)

update=$( softwareupdate -i -v "${install[*]}" >> "$logpath" )

But if i try to install more than one update it comes back with 'OS X El Capitan Update-10.11.6' 'RAWCameraUpdate6.21-6.21': No such update
if i echo "${install[*]}" in the script it looks correctly formatted and if i run the command below straight in Terminal they install with no issue.

softwareupdate -i -v 'OS X El Capitan Update-10.11.6' 'RAWCameraUpdate6.21-6.21'

Any ideas why the update binary isn't liking it when using the array variable ?

1 ACCEPTED SOLUTION

May
Contributor III

Got this working now, the main commands are below,
any advise on my scripting always greatly received, i'm not proud, luckily!

#List all updates in an array
IFS=$'
'
Available=($(softwareupdate -l | grep "*" | sed 's/^.....//'))
echo "${Available[*]}" > /private/var/tmp/updates_available.txt

#Extract only updates listed in parameters $4 to $11
Install=$( echo "${Available[*]}" | egrep "$4|$5|$6|$7|$8|$9|${10}|${11}" )
echo "$Install" > /private/var/tmp/updates_to_install.txt

#Notify user if no updates
if [[ "$Install" == "" ]]; then
#Jamfhelper here

else

echo "Updates to install - ${install[*]}"

fi

#Write to log file
echo "Begin Apple Software updates - $(date +%m_%d_%Y)" > "$logpath"
echo "$Install" >> "$logpath"

#install updates
#On 10.12+ Verbose doesn't work

osversion=$( sw_vers | grep ProductVersion | sed 's/[^0-9]*//' | cut -c1-5 | sed -e 's/.//g' )
if [[ "$osversion" -lt "1012" ]]; then
update=$( softwareupdate -i -v $Install >> "$logpath" )
else
update=$( softwareupdate -i $Install >> "$logpath" )
fi

exit 0

View solution in original post

3 REPLIES 3

JasonAtCSUMB
Contributor

I have only used an array in a loop, so I am not sure how it would react where the binary expects a string parameter. Either way, you should try debugging your script by adding

set -x

before the softwareupdate commands so you can see exactly how it is expanding the variables.

May
Contributor III

Thanks @jfilice_at_csumb very useful

Using set - x it shows

++ softwareupdate -i -v ''''OS X El Capitan Update-10.11.6''' '''RAWCameraUpdate6.21-6.21''''

it must be picking up those extra double quotes and somewhere ?

May
Contributor III

Got this working now, the main commands are below,
any advise on my scripting always greatly received, i'm not proud, luckily!

#List all updates in an array
IFS=$'
'
Available=($(softwareupdate -l | grep "*" | sed 's/^.....//'))
echo "${Available[*]}" > /private/var/tmp/updates_available.txt

#Extract only updates listed in parameters $4 to $11
Install=$( echo "${Available[*]}" | egrep "$4|$5|$6|$7|$8|$9|${10}|${11}" )
echo "$Install" > /private/var/tmp/updates_to_install.txt

#Notify user if no updates
if [[ "$Install" == "" ]]; then
#Jamfhelper here

else

echo "Updates to install - ${install[*]}"

fi

#Write to log file
echo "Begin Apple Software updates - $(date +%m_%d_%Y)" > "$logpath"
echo "$Install" >> "$logpath"

#install updates
#On 10.12+ Verbose doesn't work

osversion=$( sw_vers | grep ProductVersion | sed 's/[^0-9]*//' | cut -c1-5 | sed -e 's/.//g' )
if [[ "$osversion" -lt "1012" ]]; then
update=$( softwareupdate -i -v $Install >> "$logpath" )
else
update=$( softwareupdate -i $Install >> "$logpath" )
fi

exit 0