PlistBuddy - 添加数组项在终端中工作,但在bash脚本中不起作用

时间:2016-09-25 22:43:15

标签: xcode bash plist plistbuddy

我尝试使用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>

为什么会发生这种情况以及为什么只有命令向数组添加条目?我在许多其他地方使用plistbuddy(在bash脚本中)和设置,添加和删除简单字段等命令都可以正常工作。 / p>

我在脚本编写方面不是很好,所以很可能我错过了某种逃避或其他特定细节。

0 个答案:

没有答案