用于在plist文件中查找多个字符串的脚本

时间:2016-11-01 21:00:46

标签: bash macos awk

我试图在Mac计算机上找到当前用户是否启用了iCloud Documents。我找到了它所在的plist(MobileMeAccounts.plist),但我可以使用一些帮助创建一个脚本,可以识别它是否已启用。

我特意寻找以下代码:

<key>Enabled</key>
<true/>

这是plist。如果向下滚动,您将看到启用它的“MOBILE_DOCUMENTS”:

<?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>Accounts</key>
<array>
  <dict>
    <key>AccountAlternateDSID</key>
    <string>99999999</string>
    <key>AccountDSID</key>
    <string>999999</string>
    <key>AccountDescription</key>
    <string>iCloud</string>
    <key>AccountID</key>
    <string>*****@gmail.com</string>
    <key>AccountUUID</key>
    <string>9999999</string>
    <key>DisplayName</key>
    <string>User Name</string>
    <key>LoggedIn</key>
    <true/>
    <key>Services</key>
    <array>
        <dict>
            <key>Name</key>
            <string>CLOUDDESKTOP</string>
            <key>ServiceID</key>
            <string>com.apple.Dataclass.CloudDesktop</string>
            <key>status</key>
            <string>active</string>
        </dict>
        <dict>
            <key>Name</key>
            <string>FAMILY</string>
            <key>ServiceID</key>
            <string>com.apple.Dataclass.Family</string>
            <key>showManageFamily</key>
            <true/>
        </dict>
        <dict>
            <key>Enabled</key>
            <true/>
            <key>Name</key>
            <string>MOBILE_DOCUMENTS</string>
            <key>ServiceID</key>
            <string>com.apple.Dataclass.Ubiquity</string>
            <key>apsEnv</key>
            <string>production</string>
            <key>authMechanism</key>
            <string>token</string>
            <key>url</key>
            <string>https://p48-ubiquity.icloud.com:443</string>
            <key>wsUrl</key>
            <string>https://p48-ubiquityws.icloud.com:443</string>
        </dict>

1 个答案:

答案 0 :(得分:0)

Python包含一个用于解析plist的模块。可能你会想要一些更好的错误检查,但要证明:

$ cat parseplist.py
import plistlib
pl = plistlib.readPlist("the_plist.xml")
print pl['Accounts'][0]['Services'][2]['Enabled']

$ python parseplist.py 
True