×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

linux nested for loops HELP ..

linux nested for loops HELP ..

linux nested for loops HELP ..

(OP)
Hello all,

Need some help here ..

country=(argentina brazil china)

for (( i=0; i < ${#country[i]}; i++ )); do
for init in $(cat ~/files/zones.txt); do
echo ${country[i]} `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

RE: linux nested for loops HELP ..

Hi

Hard to got the point there. Could you show us the content of ~/files/zones.txt ?

Feherke.
feherke.github.io

RE: linux nested for loops HELP ..

Your inner loop shouldn't be a loop. It should maybe be a grep to get just the country you want.

But as feherke says, what's in zones.txt?

RE: linux nested for loops HELP ..

(OP)
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

RE: linux nested for loops HELP ..

Hi

Well, then you can not use cat 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 :

CODE --> Bash

country=(argentina brazil china)

exec 3<~/files/zones.txt # open file for reading only once at the beginning

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

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

exec 3<&- # close the file at the end 

Alternatively you could read the entire zones.txt into another array, then use the same loop control variable to get its matching item :

CODE --> Bash

country=(argentina brazil china)

init=(`cat ~/files/zones.txt`)

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

Feherke.
feherke.github.io

RE: linux nested for loops HELP ..

(OP)
Thanks! Works almost perfectly except for i < ${#country[i]}; 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

RE: linux nested for loops HELP ..

Hi

Quote (rigstars2)

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 --> Bash ( fragment )

for (( i=0; i < ${#country[@]}; i++ )); do 

Feherke.
feherke.github.io

RE: linux nested for loops HELP ..

(OP)
haha .. of course. I can't believe I missed that! I just kept staring at it for the longest time ..

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close