Re: Retrieve an element from an array
mystiq_at_risp.pl
Date: 04/23/05
- Next message: Bill Marcum: "Re: Selecting the best ftp mirror automatically."
- Previous message: Sven Mascheck: "redirection (was: find)"
- In reply to: Chris F.A. Johnson: "Re: Retrieve an element from an array"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sat, 23 Apr 2005 01:21:37 +0200
I've done it that way....seemed to work.
#!/bin/sh
INTIF[0]="eth0"
INTIF[1]="eth1"
INTIF[2]="eth2"
INTIF[3]="eth3"
INTIF[4]="eth4"
INTIF0INTIP[0]="192.168.0.1"
INTIF0INTIP[1]="192.168.0.2"
INTIF1INTIP[0]="192.168.1.1"
INTIF1INTIP[1]="192.168.1.2"
INTIF2INTIP[0]="192.168.2.1"
INTIF2INTIP[1]="192.168.2.2"
INTIF3INTIP[0]="192.168.3.1"
INTIF3INTIP[1]="192.168.3.2"
INTIF4INTIP[0]="192.168.4.1"
INTIF4INTIP[1]="192.168.4.2"
i=0
for INTIF in ${INTIF[*]} ; do
INTIFNR[$i]=`tr -d "eth" <<< $INTIF`
let i++
done
i=0
for INTIFNR in ${INTIFNR[*]} ; do
INTIFINTIP="INTIF $INTIFNR INTIP[*]"
INTIFINTIP=`tr -d " " <<< $INTIFINTIP`
INTIFIP[$i]=${!INTIFINTIP}
let i++
done
echo ${INTIFIP[*]}
bash-2.05b$ ./test.sh
192.168.0.1 192.168.0.2 192.168.1.1 192.168.1.2 192.168.2.1 192.168.2.2
192.168.3.1 192.168.3.2 192.168.4.1 192.168.4.2
bash-2.05b$
Anyway big thanks for you help Chris I'm greatly appreciate it :)
-- Best Regards, MystiQ __________ Contact info: E-mail: mystiq@risp.pl Website: http://www.audio-vault.net Phone: +48897415948 "Chris F.A. Johnson" <cfajohnson@gmail.com> wrote in message news:9jrlj2-97o.ln1@rogers.com... > On Thu, 21 Apr 2005 at 22:42 GMT, <mystiq@risp.pl> wrote: > > Hello Everyone, > > > > I've a question and in hope that someone will help me resolving the > > problem...Or is this even possible? So what I'm trying to do... > > > > I'm writing my personal firewall from scratch using bash 2.05b and > > iptables...The script I'm attempt to write must set automatically default > > rules for ips (defined in the arrays), respective interface and network or > > set more restrictive rules for individual ips (but that later as I'll figure > > out how to solve the problem with retrieving an array called by a string > > joined from splitted ones). Here's some code of mine: > > > > #Number of interfaces to maintain by firewall > > INTIFCOUNT=4 > > > > # The lines below defines the interface of which the firewall will take care > > of. > > INTIF[0]="eth0" > > INTIF[1]="eth1" > > INTIF[2]="eth2" > > > > #The lines below defines internal network ips of whose firewall should take > > look for. > > INTIF0INTIP[0]=192.168.0.1 > > INTIF0INTIP[1]=192.168.0.2 > > INTIF1INTIP[0]=192.168.1.1 > > INTIF1INTIP[1]=192.168.1.2 > > > > checkinterface () { > > i=0 > > while [ $INTIFCOUNT != $i ] ; do > > INTIFINTIP[$i]="INTIF $i INTIP[*]" # This split each string , $i will > > increase interface number, [*] will take all ips from this current interface > > INTIFINTIP[$i]=`tr -d " " <<< ${INTIFINTIP[$i]}` # join the whole string > > together but with set $i interface number set in the previous above line > > let i++ > > done > > } > > You can do the same thing with: > > checkinterface () { > i=0 > while [ $INTIFCOUNT != $i ] ; do > INTIFINTIP[$i]="INTIF${i}INTIP[*]" > i=$(( $i + 1 )) > done > } > > ...but that's not what you really want. In fact, I'm not sure just > what you do want, because the loop will produce (among other > things) INTIF3INTIP[*], and you haven't defined any variable or > array with that name. > > > > checkinterfaces > > > > echo "List of ips: " > > echo "${INTIFINTIP[*]}" > > > > This gave me the result: > > > > List of ips: > > INTIF0INTIP[*] INTIF1INTIP[*] INTIF2INTIP[*] INTIF3INTIP[*] > > Which is exactly what you told it to put into the array. > > > Instead of: > > > > List of ips: > > 192.168.0.1 192.168.0.2 192.168.1.1 192.168.1.2 > > How does that relate to what you told it to do? > > > which I call directly by: > > > > echo "${INTIF0INTIP[*]}" > > > > The problem lies in the way how I do define an array INTIF0INTIP[1] - the > > number "0" inside the string is not taken as an index number in an > > array, > > INTIF0INTIP[1] is not an array; it's an element in an array. > > > so > > I tryed > > to split the whole string INTIF0INTIP[1] into INTIF $n INTIP[*] - note the > > "$n" will increase dependably on the number of interfaces defined , and the > > "[*]" > > You probably want to generate these four instructions in the loop: > > INTIFINTIP[0]=${INTIF0INTIP[0]} > INTIFINTIP[1]=${INTIF0INTIP[1]} > INTIFINTIP[2]=${INTIF1INTIP[0]} > INTIFINTIP[3]=${INTIF1INTIP[1]} > > Unless the number of interfaces is going to change, why not just > do it like that? > > If the number of interfaces is going to change, you need to > refine your logic. > > > will do the job a getting all ips from this particular interface. Maybe > > someone has the other idea how to make this part in another approach? > > > > Any help would be greatly appreciated :) > > > -- > Chris F.A. Johnson http://cfaj.freeshell.org/shell > =================================================================== > My code (if any) in this post is copyright 2005, Chris F.A. Johnson > and may be copied under the terms of the GNU General Public License
- Next message: Bill Marcum: "Re: Selecting the best ftp mirror automatically."
- Previous message: Sven Mascheck: "redirection (was: find)"
- In reply to: Chris F.A. Johnson: "Re: Retrieve an element from an array"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|