title: Storing bash associate-array's values in while loop
body: <p>I'm trying to write a script that will scan for bluetooth devices and keep track of the name, mac and time the device was first and last seen. I'm running into an issue storing the data into the associated arrays between scans.</p>
  
  <p>On the second scan, I would have thought the arrays would be populated but they aren't. So I'm not able to know the first time I saw a device. I'm pretty sure the problem is the data is being stored into a local version of the arrays and not global but I'm not sure how to fix it. </p>
  
  <p>This is my first attempt at anything beyond very basic shell scripts so any suggestions to any part of the script would be appreciated. I have been googling as I run into problems and have no doubt I'm doing something not 100% correct or efficient.</p>
  
  <pre><code>
  
  
      #!/bin/bash
  
      declare -A bt_name
      declare -A last_seen
      declare -A first_seen
  
      while [ 1 ] ; do
              echo ""> ../data/bt_host.log
              date=$(date +%s)
              hcitool -i $1 scan| grep -v Scanning | sed "s/\t/$date, /" | sed "s/\t/, /" | while IFS="," read -r time mac name
              do
                      #debug to see if array values are there from last loop
                      echo "PREFILL-mac: $mac first: ${first_seen[$mac]} name: ${bt_name[$mac]} last: ${last_seen[$mac]}"
  
                      #populate arrays
                      bt_name["$mac"]="$name"
                      last_seen["$mac"]="$time"
  
                      #test if have seen this device before or not
                      if [[ ! ${first_seen[$mac]} ]]; then
                              first_seen["$mac"]="$time"
                              echo "Setting first"
                      fi
  
                      #resulting array values
                      echo "POSTFILL-mac: $mac first: ${first_seen[$mac]} name: ${bt_name[$mac]} last: ${last_seen[$mac]}"
  
              done
              sleep 10
      done
  
  </code></pre>
  
  <p>Thanks for the help.</p>
  
  <p>UPDATE:
  Found the problem (of coarse right after I ask). It's an issue with the process substitution in my while loop.</p>
  
  <p>I changed it to</p>
  
  <pre>
  
      while IFS="," read -r time mac name
      do
              #echo "$mac, $name, $time"
              echo "PRE-mac: $mac first: ${first_seen[$mac]} name: ${bt_name[$mac]} last: ${last_seen[$mac]}"
              bt_name["$mac"]="$name"
              last_seen["$mac"]="$time"
              if [[ ! ${first_seen[$mac]} ]]; then
                      first_seen["$mac"]="$time"
                      echo "Setting first"
              fi
  
              echo "POST-mac: $mac first: ${first_seen[$mac]} name: ${bt_name[$mac]} last: ${last_seen[$mac]}"
      done </pre>
  
  <p>And that fixed the problem.</p>
  
  <p>If there are any other improvements/suggestions people have, they would be much appreciated.</p>
  
  <p>Sorry for being one search early on asking question.</p>
  
  <p>Thanks for your time.</p>