1

I've searched and searched on here for an answer, and I think the combination of being a noob at bash, not knowing the right way to do this, and not searching for the right keywords has meant I can't quite get over the last hurdle.

I'm writing a basic bash script to create config lines into a file based on the contents of a comma separated array variable. It works perfectly for single variables, but when I try and combine the two array variables it doesn't quite work. I've tried looping within the loop and that just creates the lines one after the other when I want it to loop with the other 5 lines it creates.

The two variables have the same number of iterations. Eg:

  • arr1=SITENAME1,SITENAME2,SITENAME3
  • arr2=12:ac:23:bf:12:ca,22:de:a2:bf:21:ac,01:e4:32:f0:12:c4

and I want to pass them together into the loop:

  • SITENAME1 ($y) with 12:ac:23:bf:12:ca ($x)
  • SITENAME2 ($y) with 22:de:a2:bf:21:ac ($x)
  • SITENAME3 ($y) with 01:e4:32:f0:12:c4 ($x)

Here is the code extract:

arr1=$(echo $sites | tr "," "\n")
arr2=$(echo $mastermacs | tr "," "\n")
IFS="," read -a arr1 <<< "$sites"
IFS="," read -a arr2 <<< "$mastermacs"
for y in "${!arr1[@]}"; do
#    for x in "${arr2[@]}"; do
  n=$(($n+1))
  echo "config flexconnect group ${arr1[y]} add"$'\r' >> Flex-cfg.txt
  echo "config flexconnect group ${arr1[y]} ap add ${arr2[x]} "$'\r' >> Flex-cfg.txt
  echo "config flexconnect group ${arr1[y]} predownload master ${arr1[y]}_AP01"$'\r' >> Flex-cfg.txt
  echo "config flexconnect group ${arr1[y]} predownload enable"$'\r' >> Flex-cfg.txt
  echo "config flexconnect group ${arr1[y]} predownload start primary"$'\r' >> Flex-cfg.txt
 #    done
 done

It's this line in particular that I'm trying to iterate with 2 values (incidentally I've used it with just $y and $x as well, I've just been tinkering):

echo "config flexconnect group ${arr1[y]} ap add ${arr2[x]} "$'\r' >> Flex-cfg.txt

But as it stands it just puts the first one each time. Value Y is a name, and value X is a MAC Address.

I'm either very close or completely and utterly wrong. So could do with some kind help!

danpez
  • 13
  • 3
  • Can you show what's inside arr1 and arr2? – choroba Apr 30 '14 at 09:17
  • arr1 is just string of alpha chars. arr2 is MAC address. – danpez Apr 30 '14 at 10:14
  • One problem with this code is that `arr1` and `arr2` aren't actually arrays; they're just strings with embedded newlines. `arr1=( $(echo $sites | tr "," "\n") )` would be more (although not completely) correct. (Although the following `read` statement overwrites the value of `arr1`, so the initial assignment is moot.) – chepner Apr 30 '14 at 11:27
  • thanks chepner. Yes, there are probably some odd lines in there as I've chopped and changed a bit. The tr "," does the same as the IFS line. The main problem I have is I can't get the two variables to loop together in parallel... – danpez Apr 30 '14 at 11:45

1 Answers1

1
IFS=, read -a sz <<< "$sites"
IFS=, read -a mz <<< "$mastermacs"
fx='config flexconnect group'
for x in ${!sz[*]}
do
  echo "$fx ${sz[x]} add"
  echo "$fx ${sz[x]} ap add ${mz[x]}"
  echo "$fx ${sz[x]} predownload master ${sz[x]}_AP01"
  echo "$fx ${sz[x]} predownload enable"
  echo "$fx ${sz[x]} predownload start primary"
  echo
done
  • Don’t use arr1. You can get lost in the code pretty quick if your variable names have no meaning

  • I added extra empty echo, to help readability in the output

Zombo
  • 1
  • 62
  • 391
  • 407
  • Thanks Steven for the prompt reply. What I'm trying to achieve is to cycle through each site and MAC together. Ie: First in $sites is one loop with first MAC in $mastermacs. Second in $sites is 2nd loop with second in $mastermacs etc. There's probably a better way of doing it, but I'm new to this stuff so a bit like a blind man fumbling for his walking stick... – danpez Apr 30 '14 at 10:17
  • sorry Steven, I should have mentioned, not quite. Just as I had with the two loops, it repeats all MACs in $mastermac for each single site in $sites. Where as what I'm trying to do is align each site with each MAC. – danpez Apr 30 '14 at 10:26
  • Yes, the two variables have the same number of iterations. I've updated my explanation, apologies if that wasn't clear. – danpez Apr 30 '14 at 10:40
  • Awesome Steven. Works great, thank you! I wasn't far away it seems which gives me a bit of confidence! :) – danpez May 02 '14 at 06:27