Re: Push a plist file to all users - ownership question
Re: Push a plist file to all users - ownership question
- Subject: Re: Push a plist file to all users - ownership question
- From: Chris Devers <email@hidden>
- Date: Wed, 3 Nov 2010 10:46:06 -0400
I've used a routine something like this in the past (adapted from a slightly different context, but same idea):
plist_ref="Library/Preferences/com.mypreferences" # <-- NOTE: no .plist extension here!
for dsuser in $(dscl . -list /Users | sort)
do
# skip system accounts with UIDs below 500:
dsuid=$(dscl . read /Users/$dsuser UniqueID | awk '{print $2}')
if [ $dsuid -lt 500 ]; then
continue
fi
# look up the home folder, which isn't necessarily under "/Users"
dshome=$(dscl . read /Users/$dsuser NFSHomeDirectory | awk '{print $2}')
plist="$dshome/$plist_ref"
# stash the defaults checker command, which we'll use a couple of times
pl_dsstore_cmd="defaults read $plist SettingGoesHere 2>/dev/null || echo 'not set'"
# verify the setting before asserting changes
pl_dsstore_pre=$($pl_dsstore_cmd)
# make the change, then give a moment for it to commit to disk
su -l $dsuser defaults write $plist SettingGoesHere value
sleep 1
# verify the setting after asserting changes
pl_dsstore_post=$($pl_dsstore_cmd)
echo "For $dsuser (home folder $dshome):"
echo "Before applying changes: $pl_dsstore_pre"
echo "After applying changes: $pl_dsstore_post"
done
If you're just trying to assert a couple of key/value settings that are suitable for a `defaults write ...` command, then the approach above is fine.
If it gets a bit more complicated than that (nested data structures, etc) then /usr/libexec/PlistBuddy is more flexible, but also more obtuse.
Or, of course, you can just copy the file as you're talking about, with something like this:
plist_ref='/System/Library/User Template/English.lproj/Library/Preferences/com.mypreference.plist'
for dsuser in $(dscl . -list /Users | sort)
do
# skip system accounts with UIDs below 500:
dsuid=$(dscl . read /Users/$dsuser UniqueID | awk '{print $2}')
if [ $dsuid -lt 500 ]; then
continue
fi
# look up the home folder, which isn't necessarily under "/Users"
dshome=$(dscl . read /Users/$dsuser NFSHomeDirectory | awk '{print $2}')
plist="$dshome/$plist_ref"
# look up the primary group for this user
dsgid=$(dscl . read /Users/$dsuser PrimaryGroupID | awk '{print $2}')
# copy the file to their home folder, then update the user & group ownership
/usr/bin/ditto "$plist_ref" "$dshome/Library/Preferences/"
chown $dsuid:$dsgid "$dshome/Library/Preferences/com.mypreference.plist"
done
Hope this helps.
The rationale for using dscl this way is that you can't depend on the home folder always being /Users/foo. That might be true 99% of the time, but that assumption won't work for anyone using network accounts, or anyone that has moved their home folder to another partition or drive etc.
On the other hand, if you look up the NFSHomeDirectory parameter from Directory Services, you'll never go wrong -- and while you're at it, you can look up their UID & GID parameters to use for the chown command, too.
--
Chris Devers
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Installer-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden