从 plutil 获取信息

问题描述 投票:0回答:6

我无法从 plutil 获取信息。 我想检查 .plist 是否包含密钥

CFBundleShortVersionString
。 我认为 plutil 没有任何选项来测试密钥是否存在,所以我想我会
plutil -show file.plist >file.txt
但这不起作用。 :/ 我尝试将 plist 文件定向到带有转储选项
plutil -dump file.plist >file.txt
的文件,但没有成功。 :/ 我还尝试将 stdout 定向到 stderr 并将 stderr 和 stdout 定向到文件。 什么都没起作用。 我该怎么做?

ios bash shell
6个回答
17
投票

Oneliner 不依赖额外的实用程序来安装:

适用于 macOS Monterey 及更高版本

plutil -extract CFBundleShortVersionString raw -o - ./Info.plist

适用于 macOS Big Sur 及更早版本(无原始格式类型

plutil -extract CFBundleShortVersionString xml1 -o - ./Info.plist | sed -n "s/.*<string>\(.*\)<\/string>.*/\1/p"

11
投票

如果您需要测试 .plist 中是否存在

CFBundleShortVersionString
键,最好使用
PlistBuddy
,如下所示:

/usr/libexec/PlistBuddy -c "Print :CFBundleShortVersionString" 1.plist || echo "CFBundleShortVersionString doesn't exist"

3
投票

plutil -extract CFBundleShortVersionString xml1 -o - App-Info.plist
命令打印出
CFBundleShortVersionString
属性的内容


0
投票

为了回答你的问题,你可以创建一个包含以下内容的小 bash 脚本:

#!/bin/bash

cp $1 /tmp/$$.tmp
plutil -convert xml1 /tmp/$$.tmp
cat /tmp/$$.tmp
rm /tmp/$$.tmp

如果您调用 bash 脚本 pldump,则使用

chmod +x pldump
使其可执行。 将其放在路径中的某个位置并像这样使用它:

tlh-m0290:Preferences paul.downs$ ./pldump com.example.plist  
<?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>station.data.downloaded</key>
    <true/>
 </dict>
 </plist>

我看不出有其他方法可以将 plutil 输出到标准输出。


0
投票
$ plutil -show StorePurchasesInfo.plist 2>&1 | grep cbk

返回 plist 中包含文本“cbk”的所有行。由于某种原因

plutil
将其输出发送到 stderr。上面的代码将 stderr 重定向到 stdout,因此它可以成功通过管道传输到
grep
(或重定向到文件,或任何你想要的)。


0
投票

在 macOS Monterey 中,您可以通过检查返回状态来测试 keypath 是否存在并且是一个字符串。

  • 1
    = 测试失败
  • 0
    =通过了
#!/bin/zsh

plutil -extract CFBundleShortVersionString raw -expect string Info.plist 2>/dev/null 1>/dev/null

if [ $? -eq 0 ]; then
    echo "Exists"
else
    echo "Does not exist"
fi

当然,如果发生其他问题,例如打开文件时出现问题,这也会返回

1

根据您的需要,类似的方法是询问

plutil
该键路径上的值的类型是什么。

#!/bin/zsh

typeset typeofvalue=$(plutil -type CFBundleShortVersionString Info.plist 2>/dev/null)

if [ "string" = $typeofvalue ]; then
    echo "Exists"
else
    echo "Does not exist"
fi
© www.soinside.com 2019 - 2024. All rights reserved.