如何使用命令subtiution使用PlistBuddy

时间:2017-06-14 08:13:03

标签: bash shell plistbuddy

我正在尝试以下脚本

#!/bin/bash

OUTPUT="$(cat /Users/admin/Desktop/plist-script-output/keys-updated.txt | sed 's/"//g; s/^/-c "Print :/g; s/$/"/g' | tr '\n' ' ')"

FILE="/Users/admin/Desktop/plist-script-output/plist-data/data.plist"

PLISTBUDDY=$(/usr/libexec/PlistBuddy $OUTPUT $FILE 2>&1)
echo "$PLISTBUDDY"

上述脚本的输出是无法识别的命令

OUTPUT变量的值是

-c"打印:Ant-Conversion" -c"打印:Newitem" -c"打印:区域" -c"打印:联系"

2>& 1 这是添加的,以便打印错误(不存在的键)和正确的输出。

keys-updated.txt 包含要从plist文件中提取的键列表(不一定都是plist中存在的键)

解决方案(不工作)

尝试了@Nahuel的解决方案。但行

PLISTBUDDY = $(eval set - $ OUTPUT; / usr / libexec / PlistBuddy" $ @"" $ FILE")

仅提供plist中不存在的键列表

这是我在使用@Nahuel

的解决方案后收到的输出

打印:输入,"状态",不存在

打印:输入,"通知",不存在

打印:输入," IsMvnMgrSupported",不存在

打印:条目," BuildsetFile",不存在

打印:条目," RollupClocReportToModule",不存在

打印:输入,"分支",不存在

打印:输入," Ant-Conversion",不存在

打印:输入," IndexTag",不存在

打印:输入," WO",不存在

打印:输入,"标签",不存在

打印:输入," Newitem",不存在

直接在命令行使用命令

admin:桌面管理员$ / usr / libexec / PlistBuddy -c"打印:区域" -c"打印:联系" -c"打印:电子邮件" -c"打印:语言" -c"打印:位置" -c"打印:姓名" -c"打印:笔记" -c"打印:目的" -c"打印:跟踪" -c"打印:输入" -c"打印:URL" -c"打印:状态" -c"打印:通知" -c"打印:IsMvnMgrSupported" -c"打印:BuildsetFile" -c"打印:RollupClocReportToModule" -c"打印:分支" -c"打印:Ant-Conversion" -c"打印:IndexTag" -c"打印:WO" -c"打印:标签" -c"打印:Newitem" /Users/admin/Desktop/plist-script-output/plist-data/ActiveMQ.plist

输出结果为

监控。 cucducheuneun。 cdcdcdcdc。 Java。 dvfvfvfvfvfvfv。 ActiveMQ。 cddcdcdcdc。 Apache Software Foundation的消息传递(JMS)框架 基建。 框架。 jdbcjdbcdjdcnnjn。 打印:输入,":状态",不存在。 打印:输入,":通知",不存在。 打印:输入,":IsMvnMgrSupported",不存在。 打印:输入,":BuildsetFile",不存在。 打印:输入,":RollupClocReportToModule",不存在。 打印:输入,":分支",不存在。 打印:输入,":Ant-Conversion",不存在。 打印:输入,":IndexTag",不存在。 打印:输入,":WO",不存在。 打印:输入,":标签",不存在。 打印:输入,":Newitem",不存在。 中止陷阱:6

1 个答案:

答案 0 :(得分:0)

查看sed和tr命令后。似乎/Users/admin/Desktop/plist-script-output/keys-updated.txt包含

Ant-Conversion
Newitem
Area
Contact

整个可以用bash builtins完成:

# local args arr pcmd (if inside a function)
# readarray -t arr </Users/admin/Desktop/plist-script-output/keys-updated.txt
# because readarray doesn't work on Mac
IFS=$'\n' read -d '' arr </Users/admin/Desktop/plist-script-output/keys-updated.txt

args=()
for pcmd in "${arr[@]}"; do
    args+=(-c "Print :$pcmd")
done

PLISTBUDDY=$(/usr/libexec/PlistBuddy "${args[@]}" "$FILE" 2>&1)

第一个回答:

OUTPUT='-c "Print :Ant-Conversion" -c "Print :Newitem" -c "Print :Area" -c "Print :Contact"'

引号不是语法,因为引用处理是在变量扩展之前完成的。

不安全(注入),在这种情况下使用eval

PLISTBUDDY=$(eval /usr/libexec/PlistBuddy $OUTPUT $FILE 2>&1)

暂时不能考虑更好的事情

略好一点

PLISTBUDDY=$(eval set -- $OUTPUT;/usr/libexec/PlistBuddy "$@" "$FILE" 2>&1)