Bash 3d associative array with bash3 AND multiple files

Hello again guru�s (big apologies for wall of text)

Still working on that DNS updater for my production team and while there is a ton of hit in searches i can't seem to find the answer to this.

Context:

We have apps that switch from let�s say host1 to host2. REAL basic DNS clustering if you like.

The autosys process goes like this:

  •      Connect to the DNS.
    
  •      modify Aliases files:
    
INCL_ABCprod: abcprod IN CNAME  toto1. -> autosys magic cp -> INCL_ABCprod: abcprod IN CNAME toto2.
INCL_ABCpprod:abcpprod IN CNAME toto3. -> autosys magic cp -> INCL_ABCpprod: abcpprod IN CNAME toto3.
INCL_ABCprod2:abcsprod2 IN CNAME toto2. -> autosys magic cp -> INCL_ABCprod2: abcsprod IN CNAME toto1.

Files are straight up modified by cp.

  •      Hosts\_to_named script recreates the named.conf file
    
  •      Restart named
    

Now the logical thing would be to give each application team the nsupdate commands so they have complete control over the DNS process BUT like in many companies they want US to make it work and if everything works well make the teams individually make the transition.

SOOO at first I tought�. Let�s build an associative array� Bash3 � dang.

Well that sucks�

How about this�

Bash$ cat /etc/named.data/INCL*
toto       IN           CNAME                host1
titi          IN           CNAME                host2

toto = key
host1 = value

#!/bin/bash
 
cname=($(awk '{print $1}' /etc/named.data/INCL*))
host=($(awk '{print $4}' /etc/named.data/INCL*))
 
 
for ((i=0 ; i<${#cname[@]} ; i++ )) do
        echo "==============================="
        echo "${cname[$i]}" IN "${host[$i]}"
        echo "==============================="
done

That seems to work � but it doesn�t. For a reason I can�t explain I can�t guaranty that the value is associated with the key

How can I make sure I my association is correct?

BONUS ROUND

One thing I forgot to mention is that when I�ll have my key=value I will be able to build my nsupdate request. Fine and dandy BUT thing is I have 1000 records in my aliases files�.

With the old process, flat files do not put a burden on the host. Restart dns and be done with it. With 1000 nsupdate request everytime ONE app is switched (delete the old value, prereq nxrrset and put the new in) that will probably be not very good my server (maybe not but i'll test for sure).

SO I tought� what If I check that only files that where modified today? Since autosys process does a simple basic cp.

Bash$ find /etc/named.data/ -type f -name "INCL*" -daystart -mtime 0

So normally something like this should be perfect?

results=$(find /etc/named.data/ -name "INCL*" -daystart -mtime 0)
 
cname=($(awk '{print $1}' ${results}))
real=($(awk '{print $4}' ${results}))

But again I cannot guaranty perfect key=value

If somebody has a clue that would be great!

Thanks.

---------- Post updated at 11:09 AM ---------- Previous update was at 07:59 AM ----------

What if i do .....

Include a : between my key and value and parse threw my array using parameter substitution?

That seems to work

array=($(awk '{print $1" : "$4}' /etc/named.data/*))

for toto in " ${array[@]}" ; do
   KEY=${toto%%:*}
   VALUE=${toto#*:}
done

what if you let bash read it into separate variables?

while read -r key _ _ value _; do
  echo "$key $value"
done < <(find ... -exec cat {} +)
1 Like

Works like a charm but can you describe the KEY _ _ VALUE _ ? The man page doesn't say much. Thanks!

That will read into 5 variables, of which the dummy variable "_" will consume items 2, 3, and 5+. KEY will assume the first field's content, and VALUE the fourth's.

1 Like

read splits the line into fields like word splitting (using IFS) and assigns to each name given. It is common practice to use _ as a place holder for unused fields. If I left out the final _ and the line had more than 4 fields, the remainder would end up in $value

I hope this helps

1 Like

Sure does! Thanks all!