Editing plist file using shell script

孤街浪徒 提交于 2021-02-05 20:00:29

问题


I have used pkgbuild to create a default Component Property List file. The file looks like:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-     1.0.dtd">
<plist version="1.0">
<array>
    <dict>
        <key>BundleHasStrictIdentifier</key>
        <true/>
        <key>BundleIsRelocatable</key>
        <true/>
        <key>BundleIsVersionChecked</key>
        <true/>
        <key>BundleOverwriteAction</key>
        <string>upgrade</string>
        <key>RootRelativeBundlePath</key>
        <string>MyApp.app</string>
    </dict>
</array>
</plist>

I want to modify this file by using shell script. I tried using defaults write but it didn't do anything.

What is the way to do it?(For example: I want to set BundleIsRelocatable to false)


回答1:


Also:

plutil -replace BundleIsRelocatable -bool false plistfilename.plist



回答2:


A bit late, but for the record, you just need to specify the absolute path AND add the .plist extension to the filename. If you are running your script in same directory that the plist file, your case would be translated into:

defaults write $PWD/YourPlistFilename.plist BundleIsRelocatable -bool false



回答3:


Using PlistBuddy, a simple tutorial HERE.

 /usr/libexec/PlistBuddy -c "Set :BundleIsRelocatable bool false" plistfilename.plist

It can run as ONE command line to update the key/value. I use it to update CFBundleVersion generally, which can be found in this post.




回答4:


For String use

plutil -replace NameOfProperty -string "yourNewValue" plistFileName.plist



回答5:


Use PlistBuddy!

Very simple and straight forward. Example:

/usr/libexec/PlistBuddy ComponentPropertyList.plist
Command: Set :0:BundleIsRelocatable false
Command: save
Saving...
Command: exit

Thats it! Now BundleIsRelocatable is false :D




回答6:


Using sed:

sed -i '' '/<key>BundleIsRelocatable</{n;s/true/false/;}' file.plist

If the plist is not XML, run plutil -convert xml1 file.plist first.




回答7:


The last response of Phil-CB here should be usefull.



来源:https://stackoverflow.com/questions/26201821/editing-plist-file-using-shell-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!