Get an 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 element in an array?

0
source share
1 answer

It seems to be PlistBuddyoutputting as follows:

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

That is a few lines. If you want to go to the elements Array, first you need to cut off the first and last lines:

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

Then, to read this into a Bash array, you need to surround the sub-net with $(...)another (...), for example:

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

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

+1
source

Source: https://habr.com/ru/post/1539575/


All Articles