0

I receive from "somewhere" an array of stuff that looks like a dictionary
and I need to access the value of a specific one (e.g. the value for the key "FOO")
what is the best way to access it?

Do I need to "parse" the string (or whatever it is) to dictionary or I have a shortcut?

#!/bin/bash
# GNU bash, version 4.4.20

ARR=(FOO='hello' BAR='world')

for index in "${!ARR[@]}" 
do  
    echo "${index}"

    I_WISH_WAS_A_DICTIONARY=${ARR[$index]}
    
    echo "${I_WISH_WAS_A_DICTIONARY}"

     if [ -v ${I_WISH_WAS_A_DICTIONARY[FOO]} ]; then       
        break
     fi 
     
done

here is a runnable code sample

note about foo bar syntax:

I do not have control over the syntax of FOO='hello', BAR='world'
in the real world those come from dart define
I'm trying to use those in running a "Pre-action" script on a iOS build

function entry_decode() { echo "${*}" | base64 --decode; }

IFS=',' read -r -a define_items <<< "$DART_DEFINES"


for index in "${!define_items[@]}"
do
    define_items[$index]=$(entry_decode "${define_items[$index]}");
done
Francesco Iapicca
  • 2,618
  • 5
  • 40
  • 85
  • That's JSON, use the `jq` utility to parse it. – Barmar Jun 13 '22 at 15:50
  • is it? jq command throws on it... it's also not an associative array as it misses some brackets`([FOO]='hello'` and commma(s) – Francesco Iapicca Jun 13 '22 at 16:52
  • Sorry, didn't notice that it doesn't have `{}` around the whole thing to make a JSON object. You could try wrapping it in that before calling `jq`. – Barmar Jun 13 '22 at 16:58
  • If you define `arr` with `declare -A arr=([FOO]="hello" [BAR]="world" )` then `for index in "${!arr[@]}"; do printf '%s=%s\n' "$index" "${arr[$index]}"; done` (for instance) works. See [BashGuide/Arrays - Greg's Wiki](https://mywiki.wooledge.org/BashGuide/Arrays) for information about Bash associative arrays. See [Correct Bash and shell script variable capitalization](https://stackoverflow.com/q/673055/4154375) for an explanation of why I replaced `ARR` with `arr`. – pjh Jun 13 '22 at 17:45
  • 1
    Is `FOO='hello' BAR='world'` the output of some command? – Fravadona Jun 13 '22 at 18:30
  • @pjh I don't have control over foo bar – Francesco Iapicca Jun 13 '22 at 19:02
  • @Fravadona is correct: those come from a command, added some "note about foo bar syntax" in the initial question – Francesco Iapicca Jun 13 '22 at 19:02
  • Does IOS have bash >= 4? If not, then there's no dictionary structure in bash 3 – Fravadona Jun 13 '22 at 19:17
  • Does this do anything like what you want: `arr=(FOO='hello' BAR='world'); searchkey=FOO; for kv in "${arr[@]}"; do k=${kv%%=*}; v=${kv#*=}; if [[ $k == "$searchkey" ]]; then printf '%s=%s\n' "$k" "$v"; break; fi; done` ? – pjh Jun 13 '22 at 19:21
  • 3.2 :( good catch @Fravadona I'll try to handle it with some string juggling than :) – Francesco Iapicca Jun 13 '22 at 19:22

1 Answers1

0

Thanks to @Fravadona for pointing out the limitation of bash on iOS. Here is a possible solution for whoever may end up in my shoes in the future.

ARR=(FOO=hello BAR=world)
KEY='FOO'
for DEFINE in ${ARR[@]}
do 
    echo $DEFINE
   IFS='=' read -r K V <<< $DEFINE
   if [ $K = $KEY ]; 
   then
        echo $V
        break
   fi
done
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Francesco Iapicca
  • 2,618
  • 5
  • 40
  • 85