Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

bash for loop help - nested? 1

Status
Not open for further replies.

rigstars2

Instructor
Dec 18, 2011
64
US
I'm trying to create a loop or I believe in this case nested for loops but the logic escapes me.
Can someone please assist here? My logic looks way off here ..

for ip in $(cat $ZONES_PATH/br.zone ); do ipset -A brazil $ip; done
for ip in $(cat $ZONES_PATH/cn.zone ); do ipset -A china $ip; done
for ip in $(cat $ZONES_PATH/eg.zone ); do ipset -A egypt $ip; done
for ip in $(cat $ZONES_PATH/in.zone ); do ipset -A india $ip; done


---------------------------
countries.txt file will contain - brazil china egypt india)

initial=(br cn eg in)
for (( i=0; i < ${#initial[@]}; ++i )); do
for ip in $(cat $ZONES_PATH/$initial.zone); do
for x in $(cat ~/countries.txt); do
ipset -A $x $ip
done
done
 
Hi

As your earlier questions were about Bash, I would make use of its associative array :
Bash:
[b]declare[/b] -A [navy]initial[/navy][teal]=([/teal]
    [teal][[/teal][i][green]'br'[/green][/i][teal]]=[/teal][i][green]'brazil'[/green][/i]
    [teal][[/teal][i][green]'cn'[/green][/i][teal]]=[/teal][i][green]'china'[/green][/i]
    [teal][[/teal][i][green]'eg'[/green][/i][teal]]=[/teal][i][green]'egypt'[/green][/i]
    [teal][[/teal][i][green]'in'[/green][/i][teal]]=[/teal][i][green]'india'[/green][/i]
[teal])[/teal]

[b]for[/b] country [b]in[/b] [i][green]"${!initial[@]}"[/green][/i][teal];[/teal] [b]do[/b]
    [b]while read[/b] ip[teal];[/teal] [b]do[/b]
        ipset -A [i][green]"$country"[/green] [green]"$ip"[/green][/i]
    [b]done[/b] [teal]<[/teal] [i][green]"$ZONES_PATH/${initial[$country]}.zone"[/green][/i]
[b]done[/b]

One thing to keep in mind : as in many other languages, in Bash the associative arrays does not preserve their initial order.


Feherke.
feherke.github.io
 
totally forgot about associative arrays. I'll need to read up on it and I've never implemented it before.
Thanks again feherke!
 
feherke,

I tried running your code but I get ..i'm running this on a mac ..minor changes due to bash version too..
./test2.sh: line 13: /india.zone: No such file or directory

TABLES="/Users/rigs/tables"
declare -a initial=( ['br']='brazil' ['cn']='china' ['in']='india' )

for country in "${!initial[@]}"; do
while read ip; do
echo -n "$country" "$ip"
done < "$TABLES/${initial[$country]}.zone"
done
 
Hi

[tt]declare -a[/tt] sets the variable type to indexed array ( same like just initializing it without [tt]declare[/tt] ). You need uppercase [tt]-A[/tt] option for associative array. If not supported by your Bash, then put both country code and name in the same string, then split them later :
Code:
[navy]TABLES[/navy][teal]=[/teal][i][green]"/Users/rigs/tables"[/green][/i]
[navy]initial[/navy][teal]=([/teal] [i][green]'br brazil'[/green] [green]'cn china'[/green] [green]'in india'[/green][/i] [teal])[/teal]

[b]for[/b] country [b]in[/b] [i][green]"${initial[@]}"[/green][/i][teal];[/teal] [b]do[/b]
    [b]read[/b] code name [teal]<<<[/teal] [i][green]"$country"[/green][/i]
    [b]while read[/b] ip[teal];[/teal] [b]do[/b]
        echo -n [i][green]"$name"[/green] [green]"$ip"[/green][/i]
    [b]done[/b] [teal]<[/teal] [i][green]"$TABLES/$code.zone"[/green][/i]
[b]done[/b]

Feherke.
feherke.github.io
 
feherke,

Thanks for the help! i decided to get bash 4.4 installed and use your code...works great!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top