Madhu Avinash
Madhu Avinash

Reputation: 971

Get Array of Values using plistBuddy

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

Ouput:

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

How to get the first item in the Array?

Upvotes: 3

Views: 2098

Answers (1)

janos
janos

Reputation: 124744

It seems PlistBuddy produces output like this:

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

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'

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'))

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

Upvotes: 3

Related Questions