Solved

How best to correlate the variables in two arrays?

Posted on 2015-01-06
6
82 Views
Last Modified: 2015-01-07
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
Comment
Question by:mickt
  • 3
  • 3
6 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 40533355
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
 

Author Comment

by:mickt
ID: 40533384
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
 

Author Comment

by:mickt
ID: 40533390
Sorry, missed your first section :).  cheers again.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 68

Expert Comment

by:woolmilkporc
ID: 40533410
Why grade B? Did you find my solution incomplete?
0
 

Author Comment

by:mickt
ID: 40533414
Not at all, very good solution.  Wasn't paying attention.  Can I change it?
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 40533434
Click "Request Attention" and ask the mods to reopen the question.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now