Bash Array connectin to another Array

Hello,

i have a script that i need account_number to match a name.
for exsample :

ACCOUNT_ID=(IatHG8DC7mZbdymSoOr11w KbnlG2j-KRQ0-1_Xk356s8)

and i run a loop curl requst with this the issue is that i want to know on
which account were talking about so bash will know this :

IatHG8DC7mZbdymSoOr11w  = hostname1
KbnlG2j-KRQ0-1_Xk356s8 = hostname2

so when my loop has an issue i can get the name and not the code.

ACCOUNT_ID=(IatHG8DC7mZbdymSoOr11w KbnlG2j-KRQ0-1_Xk356s8)

for account in ${ACCOUNT_ID[@]}
        do
           somecommand

echo "$ACCOUNT_ID got this results:"
     if [[ ]]
 then say somthig
fi

done

so i need the ACCOUNT_ID in this loop to be a name
because now ofcurse it shows me the code

how can i connect the array values to a name ?
is bash dictionary is my answer ?
if so can somone show me an exsample ?

thanks.

Hello batchenr,

I think you probably want to use an Associative Array, i.e an unordered list of key to value pairs. Have a look in the man bash pages for the basic information and skip forward to the Arrays section, about 90% of the way down on my display.

Associative arrays are created using declare -A array_name and you add and use values like this:-

#!/bin/bash

declare -A array_name

array_name["Key1"]="Value1"
array_name["Key2"]="Value2"
array_name["Key3"]="Value3"
array_name["Key4"]="Value4"

for testkey in Key3 Key1
do
   echo "${array_name[$testkey]}"
done

..... and this should give the output Value3 then Value1

Is this what you need, or have I missed the point?

Kind regards,
Robin

1 Like

thanks for the replay but i dont think its it.
i do belive it should be with dictionary.

doing this is too specific with the key1 key2

declare -A array_name

array_name["Key1"]="Value1"
array_name["Key2"]="Value2"
array_name["Key3"]="Value3"
array_name["Key4"]="Value4"

for testkey in Key3 Key1
do
   echo "${array_name[$testkey]}"
done

i want it to go throw all the account codes and know that code=name
and echo the name

Methinks rbatte1 has pointed you in exactly the right direction; you just need to extrapolate / adapt his proposal.

declare -A names=([IatHG8DC7mZbdymSoOr11w]=hostname1 [KbnlG2j-KRQ0-1_Xk356s8]=hostname2)
for account in ${ACCOUNT_ID[@]};         do echo "ACCOUNT_ID = " $account " has name " ${names["$account"]}; done
ACCOUNT_ID =  IatHG8DC7mZbdymSoOr11w  has name  hostname1
ACCOUNT_ID =  KbnlG2j-KRQ0-1_Xk356s8  has name  hostname2
2 Likes

Thank you!

i was able to do it like this :

SIP_ACCOUNT_ID=(ohXlWOHKzX8wPSA9SJ e8s-XPBeAgFGel2T3Izt)

declare -A names=([ohXlWOHKzX8wPSA9SJ ]=server2.pbx [e8s-XPBeAgFGel2T3Izt]=server1.pbx)

for account in ${SIP_ACCOUNT_ID[@]}
        do
echo  " ${names["$account"]} :"
done

WORKS! when the account has an issue it shows the name!
thanks again:p