9090
9191isQuiet=false
9292isType=false
93+ isWrite=false
94+ isForce=false
9395args=()
9496while [ " $1 " != " " ]; do
9597 # See if user specified a type
@@ -102,6 +104,7 @@ while [ "$1" != "" ]; do
102104 case " $1 " in
103105 -u | -user | --user ) targetUser=" $2 " ; shift ;;
104106 -q | -quiet | --quiet ) isQuiet=true; ;;
107+ -f | -force | --force ) isForce=true; ;;
105108 -currentHost ) host=" current" ; ;;
106109 -host ) host=" $2 " ; shift ;;
107110 -h | --help | help ) output::usage; exit ;; # quit and show usage
@@ -115,12 +118,45 @@ done
115118
116119# # region ############################################## Main Code
117120
121+ # Get Particulars
118122prefix=" sudo -u $targetUser "
119123action=" ${args[0]} "
124+ domain=" ${args[1]} "
125+ key=" ${args[2]} "
120126
121127# Determine if this will write
122128if echo " $action " | grep -q ' write\|array\|array-add\|dict\|dict-add' ; then
123129 isWrite=true
130+ # Warn about trying to overwrite Managed Preferences
131+ if ! echo " $domain " | /usr/bin/grep -q " /" ; then
132+ managedPath=" /Library/Managed Preferences/${domain} "
133+ if ! echo " $domain " | /usr/bin/grep " .plist" ; then
134+ managedPath=" ${managedPath} .plist"
135+ fi
136+
137+ if [ -f " $managedPath " ]; then
138+ # Attempt to read for same key
139+ testCommand=" $binDefaults read "
140+ [ -n " $host " ] && testCommand=" $testCommand $host "
141+ output=$( $testCommand " $managedPath " 2>&1 )
142+ retval=$?
143+ absent=$( echo " $output " | /usr/bin/grep " does not exist" )
144+ if [ -z " $absent " ]; then
145+ # If we didn't get a "does not exist" error, issue a warning.
146+ if ! $isQuiet ; then
147+ {
148+ echo " $output " | head -n 1
149+ echo " The domain/default pair of ($domain , $key ) is a Managed Preference and cannot be modified."
150+ } >&2
151+ fi
152+
153+ if ! $isForce ; then
154+ # Exit instead of attempting to write
155+ exit " $retval "
156+ fi
157+ fi
158+ fi
159+ fi
124160fi
125161
126162if [ " $action " == " write" ] && $isSingle ; then
0 commit comments