Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Bash Arrays

Posted on 2009-12-23
7
Medium Priority
?
775 Views
Last Modified: 2013-12-26
I am not sure what I am doing wrong here. It is most likely my limited knowledge of bash.

I am reading two files into arrays and trying to call them later on as variables. It works if I call them by themselves

I.E.

echo ${firstarray[$i]}
echo ${secondarray[$i]}

Would say return
George
Tsafas

but if I try

echo "${firstarray[$i]} ${secondarray[$i]}"

it is only returning Tsafas

Code is attached (The from.gt and to.gt is just name changes

For example from.gt looks like this.

Mike
Tom
Greg
Bob

to.gt looks like this

Thomas
Chris Finch
Michael
Bob Smith
while read fromline ; do
fromarray[$index]="$fromline"
let index++
done < from.gt

while read toline ; do
toarray[$todex]="$toline"
let todex++
done < to.gt


	for (( i=1; i<=23; i++ ))
	do
	echo -r "${fromarray[$i]} + ${toarray[$x]}"
        done

Open in new window

0
Comment
Question by:GTsafas
  • 4
  • 3
7 Comments
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 26114192
>> echo -r "${fromarray[$i]} + ${toarray[$x]}" <<

where does $x come from?

wmp

0
 

Author Comment

by:GTsafas
ID: 26114716
oh ignore that, its $i
0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 2000 total points
ID: 26114929
OK, some more things I see:

arrays start with element 0, so use

for (( i=0; i<=23; i++ ))

(we'll talk about the '23' later)
Afaik -r is an invalid option for bash's builtin echo. What are you trying to achieve with it?

What is the '+' sign good for? It will be displayed as character '+'. Is this what you want?
At least you don't need it for concatenation or the like.

'23' seems an arbitrary value. Rather use the number of elements. Since you have two arrays, you should take care that the number of elements is consistent between both.

count=$(( ${#fromarray[*]} - 1 ))
so the loop control statement should look like

for (( i=0; i<$count; i++ ))

(see attachment for the complete snippet)


wmp




#!/usr/bin/bash

while read fromline ; do
fromarray[$index]="$fromline"
let index++
done < from.gt

while read toline ; do
toarray[$todex]="$toline"
let todex++
done < to.gt

count=$(( ${#fromarray[*]} - 1 ))

        for (( i=0; i<$count; i++ ))
        do
        echo "${fromarray[$i]} ${toarray[$i]}"
        done

Open in new window

0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:GTsafas
ID: 26115127
I was starting the loop at 1 because I didnt need the first line, The 23 was because this is a definite amount.

and the -r was just for fun, I was using count in the array for loop but I eliminated as many variables as possible to debug. the "+" sign has no relevance.

Here is my original code, the code you posted did not work for me by the way
#!/bin/bash

index=0
todex=0

while read fromline ; do
fromarray[$index]="$fromline"
index=$(($index+1))
done < from.gt

while read toline ; do
toarray[$todex]="$toline"
todex=$(($todex+1))
done < to.gt

	for (( i=0; i<=$index; i++ ))
	do

	echo "${fromarray[$i]} + ${toarray[$i]}"

	done

Open in new window

0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 26115165
Why did it work for me then?
Maybe you have /bin/bash instead of /usr/bin/bash, but that's all.
I'll look at your code later.
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 26115433
OK,

where is your problem?

I tried your exact code on one of my systems, using inputfiles precisely as you posted them.

Except that you're printing two extra lines (guess why!), it's working.

wmp


0
 

Author Comment

by:GTsafas
ID: 26116292
So weird
Will try tomorrow ty
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

572 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