shell 从plutil获取信息

xxls0lw8  于 2022-11-16  发布在  Shell
关注(0)|答案(6)|浏览(212)

plutil的方向信息出现问题。我想检查.plist是否包含密钥CFBundleShortVersionString。我认为plutil没有任何选项可以测试密钥是否存在,所以我想我可以直接使用plutil -show file.plist >file.txt,但这样做不起作用。:/因此我尝试使用转储选项plutil -dump file.plist >file.txt将plist文件从stdout定向到file,但没有成功。:/我还尝试将stdout定向到stderr和stderr,然后将stdout定向到file。没有任何效果。我该如何操作?

qoefvg9y

qoefvg9y1#

一个不依赖于额外的实用程序来安装的行:
适用于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"
cl25kdpy

cl25kdpy2#

如果你需要测试你的.plist是否存在CFBundleShortVersionString键,最好这样使用PlistBuddy

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

gblwokeq3#

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

vawmfj5a

vawmfj5a4#

为了回答您的问题,您可以创建一个小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输出到stdout。

fbcarpbf

fbcarpbf5#

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

返回plist中包含文本'cbk'的所有行。由于某种原因,plutil将其输出发送到stderr。上面的命令将stderr重定向到stdout,这样它就可以成功地通过管道发送到grep(或者重定向到一个文件,或者任何你想要的东西)。

z31licg0

z31licg06#

在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该keypath上的值的类型。

#!/bin/zsh

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

if [ "string" = $typeofvalue ]; then
    echo "Exists"
else
    echo "Does not exist"
fi

相关问题