[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 95
  • Last Modified:

How best to correlate the variables in two arrays?

I want to correlate the variables in two arrays.  What's the simplest way of doing this?  I presume using index?  I'd also like to confirm that they both contain the same quantity of elements.

E.g. for below 10.10.12.16 correlates to 04 etc.

STORAGE=(10.10.12.16 10.10.12.20 10.10.12.22)
LVM=(04 07 08)

for i in ${STORAGE[@]}
do
echo $i >> file.txt
echo CORRESPONDING_LVM_VALUE >> file.txt
done
0
mickt
Asked:
mickt
  • 3
  • 3
1 Solution
 
woolmilkporcCommented:
Yes, use an index:

#!/bin/bash
STORAGE=(10.10.12.16 10.10.12.20 10.10.12.22)
LVM=(04 07 08)

if [[ ${#STORAGE[@]} -ne ${#LVM[@]} ]]; then
 echo "Number of elements doesn't match"
 exit
fi

n=0
while [[ n -lt ${#STORAGE[@]} ]]
 do
  echo ${STORAGE[n]} >> file.txt
  echo ${LVM[n]} >> file.txt
  ((n++))
 done
0
 
micktAuthor Commented:
Cheers

And the 2nd part of question similar to the following:

if [ ${#STORAGE[@]} != ${#LVM[@]} ]
  then
    echo "STORAGE and LVM arrays do not contain the same quantity of elements.  Exiting."
    exit 1

fi
0
 
micktAuthor Commented:
Sorry, missed your first section :).  cheers again.
0
Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

 
woolmilkporcCommented:
Why grade B? Did you find my solution incomplete?
0
 
micktAuthor Commented:
Not at all, very good solution.  Wasn't paying attention.  Can I change it?
0
 
woolmilkporcCommented:
Click "Request Attention" and ask the mods to reopen the question.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now