【问题标题】:how to use plutil to get a value?如何使用 plutil 获取值?
【发布时间】:2022-06-23 07:43:54
【问题描述】:

我需要获取“ProductVersion”的值并输出:“15.1” 我有 plutil 的版本,但无法获取值

plutil -key ProductVersion myfile //不工作

这是我的文件内容:

<?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">
<dict>
      <key>ProductVersion</key>
        <string>15.1</string>
        <key>SystemImageID</key>
        <string>3DD7870B-96BB-4BA1-9C54-306734147FFE</string>
        <key>Version</key>
        <string>Version</string>
    </dict>
</dict>
</plist>

plutil的帮助:

-help                   Print this message
-full                   Print an exhaustive list of options
-verbose                Show verbose output
-show                   Show property list data
-keys                   List top level dictionary keys
-create                 Create a new empty property list

-key keyname            Recover value for key. Multiple uses builds keypath
-value value            Set value for keypath
-remove                 Remove value at keypath
-type typeid            Type to use while setting key. Valid types are int,
                        float, bool, json, and string (default). Use json to
                        define arrays and dictionaries
-convert format         Convert each property list file to selected format.
                        Formats are xml1 and binary1 and json. Note that json
                        files are saved to filename.json

【问题讨论】:

    标签: macos plist


    【解决方案1】:

    您的 plist 文件格式错误;关闭 &lt;/dict&gt; 标记重复。这需要修复。

    macOS 12.3 附带的plutil 没有-key 选项(不再),但您可以使用PlistBuddy

    /usr/libexec/PlistBuddy -c "Print :ProductVersion" myfile
    

    【讨论】:

    • 对于这个用户对 plist 文件进行更改的罕见需求,PlistBuddy 可以解决问题;谢谢!
    【解决方案2】:

    这是您在 Mac OS 12.3 上使用 plutil 从 plist 文件中获取值的方式。

    plutil -extract "ProductVersion" raw myfile
    

    如果你需要知道值的类型,你可以先做:

    plutil -type "ProductVersion" myfile
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-06
      • 2018-01-30
      • 1970-01-01
      • 1970-01-01
      • 2020-11-12
      • 2020-07-13
      • 2016-03-03
      • 2023-03-17
      相关资源
      最近更新 更多