Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


Search this Thread
Old 04-04-2011, 03:59 PM   #1
Registered: Apr 2011
Location: Philadelphia
Posts: 154

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:

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, 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:

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:
#This portion of code takes the INFILE and assigns each line to an element in the array

# 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
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.
for nameAddr in ${hostNames[@]};
	echo $nameAddr
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:
for (( i=0; i < ${#hostNames[@]} ; i++ ));
     ./ $hostNames[$i] $hostNames[$i+1]
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,

P.S. Sorry for the LONGG question, just trying to give as much detail as possible.
Old 04-04-2011, 04:23 PM   #2
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, 08:42 PM   #3
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,431

Rep: Reputation: 1878Reputation: 1878Reputation: 1878Reputation: 1878Reputation: 1878Reputation: 1878Reputation: 1878Reputation: 1878Reputation: 1878Reputation: 1878Reputation: 1878
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:

declare -A hostnames

while read -r NAME IP
done< <(cat file | tr ':' ' ')
1 members found this post helpful.
Old 04-05-2011, 12:50 AM   #4
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,239

Rep: Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024
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.
Old 04-05-2011, 09:20 AM   #5
Registered: Apr 2011
Location: Philadelphia
Posts: 154

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


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 06:41 AM
[SOLVED] bash and arrays disca Programming 3 07-27-2010 08:26 AM
Bash Arrays Simon256 Programming 2 02-17-2009 01:39 PM
Scripting & Arrays : How to fill an array with filenames which consist of spaces? klss Linux - Software 5 01-19-2009 12:16 AM
[bash] How do I nest for arrays? blckleprd Programming 3 06-05-2008 10:49 PM

All times are GMT -5. The time now is 11:12 AM.

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