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!

linux nested for loops HELP .. 1

Status
Not open for further replies.

rigstars2

Instructor
Dec 18, 2011
64
US
Hello all,

Need some help here ..

country=(argentina brazil china)

for (( i=0; i < ${#country}; i++ )); do
for init in $(cat ~/files/zones.txt); do
echo ${country} `cat ~/lists/$init.zone| wc -l`
done
done


#desired output
argentina 2000
brazil 400
china 5000

#wrong output I keep getting
argentina 2000
argentina 400
argentina 5000
brazil 2000
brazil 400
brazil 5000
china 2000
china 400
china 5000
 
Your inner loop shouldn't be a loop. It should maybe be a [tt]grep[/tt] to get just the country you want.

But as feherke says, what's in [tt]zones.txt[/tt]?

 
The ~/files/zones.txt just contains the initials of the countries
ar
br
cz


# below are 3 the $init.zone files which are just IPs contents

# ar.zone
27.2.0.0/15
27.64.0.0/12
27.118.16.0/20

# br.zone
45.164.228.0/22
45.165.36.0/22
45.165.44.0/22
45.165.112.0/22

# cn.zone
27.126.152.0/22
36.255.60.0/22
36.255.106.0/23
36.255.244.0/22
 
Hi

Well, then you can not use [tt]cat[/tt] as that will read the entire file each time.

What you want is to iterate over the array and the file content in parallel, so must use a single loop :
Bash:
[navy]country[/navy][teal]=([/teal]argentina brazil china[teal])[/teal]

[b]exec[/b] [purple]3[/purple][teal]<[/teal]~/files/zones[teal].[/teal]txt [gray]# open file for reading only once at the beginning[/gray]

[b]for[/b] [teal](([/teal] [navy]i[/navy][teal]=[/teal][purple]0[/purple][teal];[/teal] i [teal]<[/teal] [navy]${#country[i]}[/navy][teal];[/teal] i[teal]++ ));[/teal] [b]do[/b]
    [b]read[/b] init [teal]<&[/teal][purple]3[/purple] [gray]# read from the assigned file handle[/gray]
    [gray]# so the file pointer continues from where it left afte previous read[/gray]

    echo [navy]${country[i]}[/navy] `cat [teal]~[/teal]/lists[teal]/[/teal][navy]$init[/navy][teal].[/teal]zone[teal]|[/teal] wc -l`
[b]done[/b]

[b]exec[/b] [purple]3[/purple][teal]<&[/teal]- [gray]# close the file at the end[/gray]

Alternatively you could read the entire zones.txt into another array, then use the same loop control variable to get its matching item :
Bash:
[navy]country[/navy][teal]=([/teal]argentina brazil china[teal])[/teal]

[navy]init[/navy][teal]=([/teal]`cat ~/files/zones[teal].[/teal]txt`[teal])[/teal]

[b]for[/b] [teal](([/teal] [navy]i[/navy][teal]=[/teal][purple]0[/purple][teal];[/teal] i [teal]<[/teal] [navy]${#country[i]}[/navy][teal];[/teal] i[teal]++ ));[/teal] [b]do[/b]
    echo [navy]${country[i]}[/navy] `cat ~/lists[teal]/[/teal][navy]${init[i]}[/navy][teal].[/teal]zone[teal]|[/teal] wc -l`
[b]done[/b]

Feherke.
feherke.github.io
 
Thanks! Works almost perfectly except for i < ${#country}; For whatever reason I can't think of, the code won't print anymore elements beyond 4th index value so, I had to replace the array length with an integer value i < 18 for it work with more elements.

argentina 1701
brazil 10396
china 8444
egypt 171
india 6002
iran 1482
italy 2903
korea 2020
philippines 594
russia 10497
turkey 1284
ukraine 3192
vietnam 706
romania 3399
thailand 676
germany 9295
sudan 31
syria 219
 
Hi

rigstars2 said:
For whatever reason I can't think of, the code won't print anymore elements beyond 4th index value
Oops. I missed that. Needs a small fix to compare with the array's length, not the current array item's length :
Code:
[b]for[/b] [teal](([/teal] [navy]i[/navy][teal]=[/teal][purple]0[/purple][teal];[/teal] i [teal]<[/teal] [navy]${#country[[COLOR=red yellow]@[/color]]}[/navy][teal];[/teal] i[teal]++ ));[/teal] [b]do[/b]


Feherke.
feherke.github.io
 
haha .. of course. I can't believe I missed that! I just kept staring at it for the longest time ..
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top