使用plistBuddy获取值数组 [英] Get Array of Values using plistBuddy

查看:165
本文介绍了使用plistBuddy获取值数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var="keychain-access-groups"
declare -a val=$( /usr/libexec/PlistBuddy -c "Print $var" "sample.plist")
echo ${val}
echo ${val[0]}

输出:

Array { ABCD.com.bus.NoEntitlements ABCD.com.bus.sharing }
Array { ABCD.com.bus.NoEntitlements ABCD.com.bus.sharing }

如何获得第一项

推荐答案

似乎 PlistBuddy 会产生如下输出:

It seems PlistBuddy produces output like this:

Array {
    ABCD.com.bus.NoEntitlements
    ABCD.com.bus.sharing
}

即多行。如果要进入 Array 的元素,则需要首先切掉第一行和最后一行:

That is, multiple lines. If you want to get to the elements of the Array, you need to first slice off the first and last lines:

/usr/libexec/PlistBuddy | sed -e 1d -e '$d'

接下来,要将其读入Bash数组,您需要用另一个(...)包围 $(...)子外壳,如下所示:

Next, to read this into a Bash array, you need to surround the $(...) subshell with another (...), like this:

declare -a val=($(/usr/libexec/PlistBuddy | sed -e 1d -e '$d'))

之后,您可以使用 $ {val [0]访问第一个值} ,第二个值为 $ {val [1]}

After this, you can access the first value with ${val[0]} and the second value with ${val[1]}.

这篇关于使用plistBuddy获取值数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆