I am writing a script to automate the process of setting up a bunch of Mac's in a computer lab.
Each system is uniquely identified and I need a method of patching a plist file in several locations with the same string that will be read from the user in the script which is a bash script
The original string is always the same. The patching string is variable depending on the identity of the system the script is being run on. This string is read from the user at the start of the script 开发者_StackOverflow中文版for various other purposes and stored in $macnum.
Can anybody please provide me a simple solution that can be scripted to perform the task? Thanks.
You can use some unique identifier (e.g. {{MACHINE_ID}}
) in the plist and use sed
to replace it:
sed -i -e 's/{{MACHINE_ID}}/'"$macnum"/g filename
sed -i "s/plist-macnum-placeholder/$macnum/g' file ...
Where -i
means edit the file "in-place" and /g
says make the substitution multiple times per line and can be dropped if there is only one.
The sed-based approach strager and msw gave will work fine if the plist you're changing is in XML format, but if it's in Apple's binary format it'll probably corrupt the file format. You can use plutil to convert it to XML first:
plutil -convert xml1 filename
sed -i -e "s/placeholder/$macnum/g" filename
It shouldn't be necessary to convert it back to binary format afterward, as Apple's plist frameworks read the two formats interchangeably. Another approach would be to use PlistBuddy to edit the contents of the plist (although it'll require the script to know what entries to set to what values, rather than just replacing a placeholder):
/usr/libexec/PlistBuddy -c "set :oneentry 'value including $macnum where appropriate'" filename
/usr/libexec/PlistBuddy -c "set :anotherentry 'value including $macnum where appropriate'" filename
Finally, you can do the same thing with defaults, although it requires you specify the .plist file by full path, and leave the .plist off its name:
defaults write oneentry "value including $macnum where appropriate" /path/to/filename-without-plist
defaults write anotherentry "value including $macnum where appropriate" /path/to/filename-without-plist
精彩评论