Read array from plist through shell script plist buddy

I wrote a shell script that reads an 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 get all the rows from this array, but I cannot get the count from this array.

Please, help.

+4
source share
2 answers

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

Array {
    1
    2
}

sed will delete the 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 get 1 2which you declare as an array inFILE_ARRAY

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

The length of such an array will be - echo ${#FILE_ARRAY[@]}

Response source

+1
source

Print , . 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
+1

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


All Articles