LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-21-2007, 10:16 PM   #1
jchambers
Member
 
Registered: Aug 2007
Location: California
Distribution: Debian
Posts: 127

Rep: Reputation: 15
Another Bash iterate lines to array...


Sorry for posting another simple array question but I am just not getting it.


Here is what I have.

Code:
#!/bin/bash

DNS="/var/named/chroot/etc/dns"


myArray[0]=""
grep  "^[^$|;|[:space:]].*[0-9][0-9]" $DNS/text_file_w_ips|
while read a
do 
	(( CNT++ ))
	echo $CNT ") " $a
	myArray[$CNT]=$a
done

echo
echo

for x in "${myArray[@]}"
do
	echo $x
done

echo
echo

echo 'array ==> '${myArray[2]}

exit

Here is the results.
Code:
1 )  @ IN MX 10 mail.server.com.
2 )  mail IN A 111.111.111.111
3 )  @ IN A 111.111.111.111
4 )  www IN A 111.111.111.111
5 )  ftp IN A 111.111.111.111
6 )  localhost IN A 127.0.0.1





array ==>

So you can see that I am getting results they are just not being passed to myArray[].


Does anyone see where I am going wrong on this?


Thank you,
Jon
 
Old 08-21-2007, 10:43 PM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
how does your input file look like and what are the desired output look like...? you want to use arrays so that you can use them again in your script right? If not, there's no need to use arrays.
 
Old 08-21-2007, 10:59 PM   #3
jchambers
Member
 
Registered: Aug 2007
Location: California
Distribution: Debian
Posts: 127

Original Poster
Rep: Reputation: 15
Hello.


Here is the desired output.

Code:
1 )  @ IN MX 10 mail.server.com.
2 )  mail IN A 111.111.111.111
3 )  @ IN A 111.111.111.111
4 )  www IN A 111.111.111.111
5 )  ftp IN A 111.111.111.111
6 )  localhost IN A 127.0.0.1

Enter the number you want to edit:

So let's say I entered 6.


Code:
Ok, you have enter: 6
"localhost IN A 127.0.0.1"

Enter a new A-Name:
Enter a new IP:


I have the code written after I get the array value back.

Get the picture?

I am more than happy to post all the code if it helps.

Thanks.
 
Old 08-21-2007, 11:36 PM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Looks reasonable to me - are you getting blanks by any chance ???.
Stick some diagnostic echo's in there - especially for ${myArray[@]} in and after the first loop. Make sure the array is actually being built - *then* go looking for the data error.
 
Old 08-21-2007, 11:59 PM   #5
jchambers
Member
 
Registered: Aug 2007
Location: California
Distribution: Debian
Posts: 127

Original Poster
Rep: Reputation: 15
driving me bannanas

So I think it has to do with "read", as I built another one without read and the array worked. Not correclty for what I need but worked.


Here is the newest code
Code:
myArray=(1 2 3 4 5)
grep  "^[^$|;|[:space:]].*[0-9][0-9]" $DNS/test_file|
while read -e a
do 
	(( cnt++ ))
	myArray[$cnt]="X "$a
	echo $cnt ") " $a
done


myArray[4]="TESTING"

echo
echo "myArray == > "${myArray[@]}

And the output
Code:
1 )  @ IN MX 10 mail.server.com.
2 )  mail IN A 111.111.111.111
3 )  @ IN A 111.111.111.111
4 )  www IN A 111.111.111.111
5 )  ftp IN A 111.111.111.111
6 )  localhost IN A 127.0.0.1

myArray == > 1 2 3 4 TESTING

Notice that the array takes outsied the while read. hmm...?

Thank you for the reply.
 
Old 08-22-2007, 12:19 AM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Mmmm - interesting. Should keep you busy for a while.

BTW, I meant to say "especially for ${#myArray[@]} in and after the first loop"
So you could track the array index.
D'oh ...
 
Old 08-22-2007, 02:30 AM   #7
jchambers
Member
 
Registered: Aug 2007
Location: California
Distribution: Debian
Posts: 127

Original Poster
Rep: Reputation: 15
So here is what I came up with.


Code:
#### Get current dns entry for the entered zone #
grep  "^[^$|;|[:space:]].*[0-9][0-9]" $DNS/$DOMAIN.dns |
while read a
do 
	(( cnt++ ))
	echo $cnt ") " $a
	echo $a > TMP$cnt  ## Super CheeseY #
done
Code:
#### Entry replacer or new entry #
read -p "Enter a number to edit the record or leave blank to add a new entry:" ENTRY
Code:
1 )  @ IN MX 10 mail.server.com.
2 )  mail IN A 111.111.111.111
3 )  @ IN A 111.111.111.111
4 )  www IN A 111.111.111.111
5 )  ftp IN A 111.111.111.111
6 )  localhost IN A 127.0.0.1

Enter a number to edit the record or leave blank to add a new entry:
Then I call the file up later on.

Code:
TMP=`cat TMP$ENTRY`
$ENTRY is a number

Seems to work well enough.
I clean up the files at the end.



Now... for one more ??? as I ran into another issue.


Code:
	TABS=""
	cnt=`echo 6-${#AIP}/2+1 | bc`
	for i in $cnt
	do
		TABS=$TABS$'\t'
	done
I only end up with one tab no matter what.
Is that not a valid statement?

Thanks again.
 
Old 08-22-2007, 02:54 AM   #8
jchambers
Member
 
Registered: Aug 2007
Location: California
Distribution: Debian
Posts: 127

Original Poster
Rep: Reputation: 15
Tabs now....

Fixed the tabs. (olive)

Code:
	#### Tabbing #
	TB="\t\t\t\t"
	TABS=""
	cnt=`echo 6-${#AIP}/2+2 | bc`
	for i in $cnt
	do
		TABS=`printf "$TABS\t"`
	done

	
	#### RegExp #
	TMP=`cat TMP$ENTRY`
	FILE=`echo "$TMP" | sed -e's/ /\.\*/g'`
	REPLACE="`printf $ANAME$TABS'IN'$TB'A'$TB$AIP`"
But the red code does not work.
No matter what I get one space instead of tabs.

Any ideas on this?

I feel I am really close.
 
Old 08-22-2007, 04:29 AM   #9
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
...works for me when printing the quoted value:
Code:
REPLACE=`printf ${ANAME}${TABS}IN${TB}A${TB}${AIP}`
echo "$REPLACE"| tr "      " "x"
$ INxxxxAxxxx127.0.0.2
('tr' not spaces but literal tab as in "CTRL-V + CTRL-TAB".)
 
Old 08-22-2007, 02:06 PM   #10
jchambers
Member
 
Registered: Aug 2007
Location: California
Distribution: Debian
Posts: 127

Original Poster
Rep: Reputation: 15
Holly-Molly and D-oh!



Good Code
Code:
	#### Tabbing Function #
	TABS=""
	cnt=`echo 7-${#ANAME}/2 | bc`
	i=1
	while [ $i -lt $cnt ]
	do
		TABS=${TABS}"\t"
		(( i++ ))
	done
I looked right past it...

BAD Code
Code:
	#### Tabbing Function #
	TABS=""
	cnt=`echo 7-${#ANAME}/2 | bc`
	for i in $cnt
	do
		TABS=${TABS}"\t"
	done


That's why it always returned 1 tab.

unSpawn - Thanks a bunch you got me on the right track.
 
  


Reply



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
how many lines and words (bash) sharapchi Programming 4 12-15-2006 12:45 PM
Bash lines wrapping Ephracis Linux - Newbie 2 09-26-2006 07:35 AM
c: iterate an array of structs kpachopoulos Programming 2 08-15-2006 11:06 AM
how-to: repeat OR iterate shell OR bash command delay OR interval admarshall Linux - General 5 07-18-2005 10:47 PM
Bash: Iterate through $@ (easy++) beatnik007 Programming 2 04-03-2004 01:39 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:28 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration