0

我正在尝试以下脚本

#!/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"

上述脚本的输出是Unrecognized Command

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”,不存在

关于直接在命令行上使用命令

管理员:桌面管理员 $/usr/libexec/PlistBuddy -c “打印:区域” -c “打印:联系人” -c “打印:电子邮件” -c “打印:语言” -c “打印:位置” -c “打印:名称” -c "Print :Notes" -c "Print :Purpose" -c "Print :Track" -c "Print :Type" -c "Print :URL" -c "Print :Status" -c "Print :Notify" -c "打印:IsMvnMgrSupported” -c “打印:BuildsetFile” -c “打印:RollupClocReportToModule” -c “打印:Branches” -c “打印:Ant-Conversion” -c “打印:IndexTag” -c “打印:WO” -c “打印:标签”-c “打印:Newitem”/Users/admin/Desktop/plist-script-output/plist-data/ActiveMQ.plist

输出结果是

监测。呱呱坠地 cddcdcdc 。爪哇。dvfvfvfvfvfvfv。活动MQ。cddcdcdcdc 。来自 Apache Software Foundation 的消息传递 (JMS) 框架。
基础设施 。框架 。jdbcjdbcdjdcnnjn 。打印:条目,“:状态”,不存在。打印:条目,“:通知”,不存在。打印:条目,“:IsMvnMgrSupported”,不存在。打印:条目,“:BuildsetFile”,不存在。打印:条目,“:RollupClocReportToModule”,不存在。打印:条目,“:分支”,不存在。打印:条目,“:Ant-Conversion”,不存在。打印:条目,“:IndexTag”,不存在。打印:条目,“:WO”,不存在。打印:条目,“:标签”,不存在。打印:条目,“:Newitem”,不存在。

4

1 回答 1

0

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

Ant-Conversion
Newitem
Area
Contact

整个可以用 bash 内置函数完成:

# 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)
于 2017-06-14T09:02:22.627 回答