LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Another Bash iterate lines to array... (http://www.linuxquestions.org/questions/programming-9/another-bash-iterate-lines-to-array-578828/)

jchambers 08-21-2007 10:16 PM

Another Bash iterate lines to array...
 
Sorry for posting another simple array question but I am just not getting it. :confused:


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

ghostdog74 08-21-2007 10:43 PM

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.

jchambers 08-21-2007 10:59 PM

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.

syg00 08-21-2007 11:36 PM

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.

jchambers 08-21-2007 11:59 PM

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.

syg00 08-22-2007 12:19 AM

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 ...

jchambers 08-22-2007 02:30 AM

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.

jchambers 08-22-2007 02:54 AM

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. :)

unSpawn 08-22-2007 04:29 AM

...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".)

jchambers 08-22-2007 02:06 PM

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. :D


All times are GMT -5. The time now is 12:04 PM.