使用 bash 脚本从 plist 中提取字符串,也许是 sed?
所以这是我的问题。 如何使用 bash 脚本将这些字符串提取到 3 个变量中?
所以出于这个:
<key>GameDir</key> <string>C:/Program Files/</string> <key>GameEXE</key> <string>C:/Program Files/any.exe</string> <key>GameFlags</key> <string>-anyflag</string>
我想要:
GameDir=C:/Program Files/
GameEXE=C:/Program Files/any.exe
GameFlags=-anyflag
示例脚本:
echo GameDir
echo GameEXE
echo GameFlags
示例输出:
C:/Program Files/
C:/Program Files/any.exe
-anyflag
键的顺序没有改变,只有字符串本身。 我使用的是 OS X,因此它需要是一个在 OS X 上开箱即用的命令。 也许这可以与 sed 一起使用吗?
谢谢 德拉库利克斯
So here is my problem.
How can I extract those strings into 3 variables with a bash script?
So out of this:
<key>GameDir</key> <string>C:/Program Files/</string> <key>GameEXE</key> <string>C:/Program Files/any.exe</string> <key>GameFlags</key> <string>-anyflag</string>
I want:
GameDir=C:/Program Files/
GameEXE=C:/Program Files/any.exe
GameFlags=-anyflag
Example Script:
echo GameDir
echo GameEXE
echo GameFlags
Example Output:
C:/Program Files/
C:/Program Files/any.exe
-anyflag
The order of the keys is not changing, only the strings itself.
I am using OS X, so it needs to be a command that works out-of-the-box on OS X.
Maybe this could work with sed?
Thanks
Drakulix
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
/usr/libexec/PlistBuddy
来实现此目的,它似乎至少从 10.5 开始就包含在 OS X 中(手册页)。示例:您可以像这样分配您感兴趣的变量:
它似乎比 bash/sed/awk 正则表达式更具可读性,并且由于它是特定于 plist 的工具,我认为它对于提取值来说是可靠的。请注意,按键区分大小写。
You could use
/usr/libexec/PlistBuddy
for this, which seems to be included in OS X since at least 10.5 (man page). Example:And you could assign the vars you're interested in like this:
It seems more readable than bash/sed/awk regexes, and since it is a plist-specific tool, I presume it is reliable for extracting values. Just be aware that the keys are case-sensitive.
假设 Bash 版本 >= 3.2
或
和
Assuming Bash version >= 3.2
or
and
您可以使用 awk,这适用于多行和多个键、字符串值。
使用 eval 创建变量
you can use awk, this works for multiline and multiple key, string values.
Use eval to create the variables