LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 04-04-2011, 04:59 PM   #1
mrm5102
Member
 
Registered: Apr 2011
Location: Philadelphia
Posts: 158

Rep: Reputation: 3
Bash Scripting - Using Arrays


Hi all,

I'm writing a bash script that executes a few perl scripts. One of the perl scripts that I need to execute requires two arguments with it.

The arguments are stored in a txt file, each line contains a hostname and its corresponding IP address separated by a ":" (colon), the txt file looks like this below:

---hosts.txt---
host123PHI:192.168.1.0
host456ATL:192.168.1.1
host789BAL:192.168.1.2
host111BIS:192.168.1.3
host212CAL:192.168.1.4
more hosts etc.....

I'm not sure if it's the best way to accomplish this but here it goes.
In the bash file, let's call it getHosts.sh, I create an array and assign each line of the file to an element in that array. I then think I need to create a new array where I take the hostname (which is before the ":") separate it from its IP address and place the IP address on a new line just below the hostname (this way I can reference to it like $hostNames[$x] would be the hostname, and $hostNames[$x+1] would be its IP address). So the new array would now look like this below:

host123PHI
192.168.1.0
host456ATL
192.168.1.1
host789BAL
192.168.1.2
host111BIS
192.168.1.3
host212CAL
192.168.1.4
more hosts etc.....

If there is a better way to attack this please let me know, as I am a beginner to bash shell scripting.
Here is what I have so far:
Code:
#This portion of code takes the INFILE and assigns each line to an element in the array
INFILE=/dir/location/hosts.txt

# Declare array
declare -a hostNames
# Link filedescriptor 10 with stdin
exec 10<&0
# stdin replaced with a file
exec < $INFILE
let count=0
while read LINE; do
	hostNames[$count]=$LINE
	((count++))
done
The code portion above works, but now I would like to separate each element on the ":" and push the second part of the element (IP address) to a new line/element. I was able to do that using the code below, but I am not able to make reference to an individual element.
Code:
for nameAddr in ${hostNames[@]};
do
	echo $nameAddr
done
The resulting output of this portion of code above is what I want.
So how can I do this so I can use a for loop that would work like this:
Code:
for (( i=0; i < ${#hostNames[@]} ; i++ ));
do
     ./myCommandToRun.pl $hostNames[$i] $hostNames[$i+1]
done
If there is a better way for me to go at this please let me know. Any help would be greatly appreciated. Details and explanations of replies would be best as I am trying to learn.

Thanks in advance,
Matt

P.S. Sorry for the LONGG question, just trying to give as much detail as possible.
 
Old 04-04-2011, 05:23 PM   #2
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 248Reputation: 248Reputation: 248
Why don't you write a perl script that uses that text file directly ?
You can split the lines into constituent parts using the colon as the delimiter.
Then create a hash containing the same data in hostname, ip format (key, value).

You could then access each ip via it's hostname at will.

I'm not confident with hashes in bash, but you could possibly do a 2D array.
 
Old 04-04-2011, 09:42 PM   #3
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,646

Rep: Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960
Firstly I agree 100% with smoker that you should be doing it all in Perl as it will do it in its sleep.
However, as the questions is about bash I would suggest using an associative array. Assumptions here are that
you have bash v4+ and that the hostnames are unique:
Code:
#!/bin/bash

declare -A hostnames

while read -r NAME IP
do
    hostnames[$NAME]=$IP
done< <(cat file | tr ':' ' ')
 
1 members found this post helpful.
Old 04-05-2011, 01:50 AM   #4
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,311

Rep: Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040
I agree with doing it all in Perl; the less interfaces the better.
You can open the file from Perl and use the split() fn to separate the values in each record.
http://perldoc.perl.org/
 
Old 04-05-2011, 10:20 AM   #5
mrm5102
Member
 
Registered: Apr 2011
Location: Philadelphia
Posts: 158

Original Poster
Rep: Reputation: 3
Hey guys,
Thanks for the suggestions. I'm gonna try and give it a go using hashes.
 
  


Reply

Tags
array, bash


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash scripting - arrays and indirect referencing soulcurry Programming 7 04-21-2011 07:41 AM
[SOLVED] bash and arrays disca Programming 3 07-27-2010 09:26 AM
Bash Arrays Simon256 Programming 2 02-17-2009 02:39 PM
Scripting & Arrays : How to fill an array with filenames which consist of spaces? klss Linux - Software 5 01-19-2009 01:16 AM
[bash] How do I nest for arrays? blckleprd Programming 3 06-05-2008 11:49 PM


All times are GMT -5. The time now is 05:08 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration