read array from plist through shell script plist buddy
Asked Answered
P

2

7

I have written shell script which reads array from plist.

PLIST_PATH="./../Documentation/documentation.plist"
echo "PATH = $PLIST_PATH"
FILE_ARRAY=`/usr/libexec/PlistBuddy -c "print :'public-headers'" $PLIST_PATH`

Now I want to retrieve all the strings from this array but i am not able to get count from this array.

please help.

Pleiades answered 7/5, 2014 at 12:52 Comment(3)
What does that command produce?Maritzamariupol
FILE_ARRAY Value is : Array { test.h test2 35146514 }Pleiades
I want to get these values in loop.Pleiades
G
5

The safe way is to iterate thru the array with individual Print statements for the indexes until no key is found anymore. The script below prints the number of entries in the given array key and plist

PLISTBUDDY="/usr/libexec/PlistBuddy -c"
if [ "$#" -ne 2 ]; then
  echo "usage: $0 <array key> <plistfile>"
  exit 1
fi
KEY=$1
PLIST=$2
i=0
while true ; do
   $PLISTBUDDY "Print :$KEY:$i" "$PLIST" >/dev/null 2>/dev/null
   if [ $? -ne 0 ]; then
      echo $i
      break
   fi
   i=$(($i + 1))
done
Georg answered 6/7, 2018 at 12:6 Comment(0)
F
1

Any array from your command will return an array of the form -

Array {
    1
    2
}

sed will remove first and last line, so with this -

declare -a FILE_ARRAY =($(/usr/libexec/PlistBuddy -c "print :'public-headers'" $PLIST_PATH | sed -e 1d -e '$d'))

you will get 1 2, which you are declaring as an array in FILE_ARRAY

which you can access as - ${FILE_ARRAY[1]}

Length of such array will be - echo ${#FILE_ARRAY[@]}

Source of the answer

Flit answered 4/12, 2017 at 14:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.