PlistBuddy - 添加数组项可以在终端中使用,但不能在 bash 脚本中使用

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

我尝试通过脚本添加自定义字体数组字段来使用

plistbuddy
操作我的 Info.plist 文件。命令在终端中成功执行(两者:创建数组条目并添加条目):

luka$ /usr/libexec/PlistBuddy testing.plist
File Doesn't Exist, Will Create: testing.plist
Command: Add UIAppFonts array
Command: Add UIAppFonts: string test
Command: Add UIAppFonts: string Test2
Command: Save
Saving...

这会生成漂亮的 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>UIAppFonts</key>
    <array>
        <string>test</string>
        <string>Test2</string>
    </array>
</dict>
</plist>

但是在 bash 脚本中执行相同的命令时会失败 (

test.sh
):

#! /bin/bash
PLISTBUDDY="/usr/libexec/PlistBuddy -c"
FILE="./test.plist"
$PLISTBUDDY "Add UIAppFonts array" $FILE
FF_CUSTOM_FONTS="Font_a.otf,Font_b.otf"
set -f; IFS=,
    FONT_INDEX=0
    for CUSTOM_FONT in $FF_CUSTOM_FONTS
        do
            PLIST_COMMAND="Add UIAppFonts: string $CUSTOM_FONT"
            echo "executing: $PLIST_COMMAND"
            $PLISTBUDDY $PLIST_COMMAND $FILE
            FONT_INDEX=$((FONT_INDEX+1))
        done
set =f; unset IFS

在这种情况下,只创建了数组,但添加条目失败。我只得到了没有那么多的描述性错误:

luka$ ./test.sh
File Doesn't Exist, Will Create: ./test.plist
executing: Add UIAppFonts: string Font_a.otf
./test.sh: line 12: /usr/libexec/PlistBuddy -c: No such file or directory
executing: Add UIAppFonts: string Font_b.otf
./test.sh: line 12: /usr/libexec/PlistBuddy -c: No such file or directory

产生这个(只有数组,没有条目):

<?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>UIAppFonts</key>
    <array/>
</dict>
</plist>

为什么会发生这种情况,为什么只使用向数组添加条目的命令?我在许多其他地方(在 bash 脚本中)使用

plistbuddy
,并且设置、添加和删除简单字段等命令都可以正常工作。

我不太擅长编写脚本,所以很可能我错过了某种转义或其他 bash 特定的细节。

xcode bash plist plistbuddy
1个回答
0
投票

嗯,我得到了和你一样的奇怪数组:

<array/>

而不是:

<array>
</array>

并且实际的数组内容也没有被写入。我正在搞乱 Mac 和 iOS 上的网络配置:

/private/var/preferences/SystemConfiguration/preferences.plist

Mac (or iOS symlink):
/Library/Preferences/SystemConfiguration/preferences.plist

似乎 scutil 无法更改代理配置,找不到 iOS 的“networksetup”,并且 iOS GUI 设置不允许定义要绕过的例外,我想绕过“192.168/16”,因此不使用代理对于内联网。所以 PlistBuddy 说:

Add <Entry> <Type> [<Value>]

我就是这么做的!:

PlistBuddy -c "Add NetworkServices:$var2:Proxies:ExceptionsList array 192.168/16" /private/var/preferences/SystemConfiguration/preferences.plist

我得到的只是这个奇怪的数组(上图)。事实证明这必须分为两步:

PlistBuddy -c "Add NetworkServices:$var2:Proxies:ExceptionsList array" /private/var/preferences/SystemConfiguration/preferences.plist

PlistBuddy -c "Add NetworkServices:$var2:Proxies:ExceptionsList:0 string 192.168/16" /private/var/preferences/SystemConfiguration/preferences.plist

如果您有兴趣,这是我的代码:

#!/bin/bash

# first array item: ethernet
# second array item: wifi
# afterwards: CommCenter

var1=`PlistBuddy -c "Print CurrentSet" /private/var/preferences/SystemConfiguration/preferences.plist`

var1=${var1/#\/Sets\/}

echo Set: $var1

if [ -n "$1" ] && [ $1 == add ]; then
    echo ADD

    var2=`PlistBuddy -c "Print Sets:$var1:Network:Global:IPv4:ServiceOrder:1" /private/var/preferences/SystemConfiguration/preferences.plist`

    echo Key: $var2

    PlistBuddy -c "Add NetworkServices:$var2:Proxies:HTTPEnable integer 1" /private/var/preferences/SystemConfiguration/preferences.plist

    PlistBuddy -c "Add NetworkServices:$var2:Proxies:HTTPPort integer 89" /private/var/preferences/SystemConfiguration/preferences.plist

    PlistBuddy -c "Add NetworkServices:$var2:Proxies:HTTPProxy string 192.168.0.21" /private/var/preferences/SystemConfiguration/preferences.plist

    PlistBuddy -c "Add NetworkServices:$var2:Proxies:HTTPSEnable integer 1" /private/var/preferences/SystemConfiguration/preferences.plist

    PlistBuddy -c "Add NetworkServices:$var2:Proxies:HTTPSPort integer 89" /private/var/preferences/SystemConfiguration/preferences.plist

    PlistBuddy -c "Add NetworkServices:$var2:Proxies:HTTPSProxy string 192.168.0.21" /private/var/preferences/SystemConfiguration/preferences.plist

elif [ -n "$1" ] && [ $1 == remove ]; then
    echo REMOVE

    var2=`PlistBuddy -c "Print Sets:$var1:Network:Global:IPv4:ServiceOrder:1" /private/var/preferences/SystemConfiguration/preferences.plist`

    echo Key: $var2

    PlistBuddy -c "Delete NetworkServices:$var2:Proxies:HTTPEnable" /private/var/preferences/SystemConfiguration/preferences.plist

    PlistBuddy -c "Delete NetworkServices:$var2:Proxies:HTTPPort" /private/var/preferences/SystemConfiguration/preferences.plist

    PlistBuddy -c "Delete NetworkServices:$var2:Proxies:HTTPProxy" /private/var/preferences/SystemConfiguration/preferences.plist

    PlistBuddy -c "Delete NetworkServices:$var2:Proxies:HTTPSEnable" /private/var/preferences/SystemConfiguration/preferences.plist

    PlistBuddy -c "Delete NetworkServices:$var2:Proxies:HTTPSPort" /private/var/preferences/SystemConfiguration/preferences.plist

    PlistBuddy -c "Delete NetworkServices:$var2:Proxies:HTTPSProxy" /private/var/preferences/SystemConfiguration/preferences.plist
else
    echo BYPASS

    for i in $(seq 0 1)
    do
        #echo $i

        var2=`PlistBuddy -c "Print Sets:$var1:Network:Global:IPv4:ServiceOrder:$i" /private/var/preferences/SystemConfiguration/preferences.plist`

        echo Key: $var2

        # PlistBuddy -c "Print NetworkServices:$var2:Proxies:ExceptionsList" /private/var/preferences/SystemConfiguration/preferences.plist

        PlistBuddy -c "Delete NetworkServices:$var2:Proxies:ExceptionsList:1" /private/var/preferences/SystemConfiguration/preferences.plist

        #PlistBuddy -c "Add NetworkServices:$var2:Proxies:ExceptionsList array" /private/var/preferences/SystemConfiguration/preferences.plist

        #PlistBuddy -c "Add NetworkServices:$var2:Proxies:ExceptionsList:1 string 192.168/16" /private/var/preferences/SystemConfiguration/preferences.plist

        PlistBuddy -c "Set NetworkServices:$var2:Proxies:ExceptionsList:0 192.168/16" /private/var/preferences/SystemConfiguration/preferences.plist
    done
fi

unset var1
unset var2

wifiutil disable-wifi
wifiutil enable-wifi
© www.soinside.com 2019 - 2024. All rights reserved.