×
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

Jobs

bash for loop help - nested?

bash for loop help - nested?

bash for loop help - nested?

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

RE: bash for loop help - nested?

Hi

As your earlier questions were about Bash, I would make use of its associative array :

CODE --> Bash

declare -A initial=(
    ['br']='brazil'
    ['cn']='china'
    ['eg']='egypt'
    ['in']='india'
)

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

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

RE: bash for loop help - nested?

(OP)
totally forgot about associative arrays. I'll need to read up on it and I've never implemented it before.
Thanks again feherke!

RE: bash for loop help - nested?

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

RE: bash for loop help - nested?

Hi

declare -a sets the variable type to indexed array ( same like just initializing it without declare ). You need uppercase -A 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 --> shell

TABLES="/Users/rigs/tables"
initial=( 'br brazil' 'cn china' 'in india' )

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

Feherke.
feherke.github.io

RE: bash for loop help - nested?

(OP)
feherke,

Thanks for the help! i decided to get bash 4.4 installed and use your code...works great!

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