PDA

View Full Version : passing arrays to for loop in a bash script


ssalman
July 21st, 2006, 01:24 PM
Hi, I’m trying to setup a firewall blocking certain websites through a bash script using iptables based on this (https://help.ubuntu.com/community/IptablesHowTo)iptables howto.

for each blocked website I'm using the command:


iptables -A OUTPUT -d www.website.com -j DROP

Now I have many websites in a list file "block.list", and would like to have the script pass them one by one in a for loop... How would I accomplish that in a bash script?

something of the general form below:
for x in ???block.list???
do
iptables -A OUTPUT -d $x -j DROP;
done

fabiand
July 21st, 2006, 01:32 PM
Hey,

a sample:


BLOCKLIST="google.com slashdot.org"

for SITE in $BLOCKLIST;
do
iptables ..... $SITE;
done;

kabus
July 21st, 2006, 01:40 PM
Now I have many websites in a list file "block.list", and would like to have the script pass them one by one in a for loop...

Why not a while loop?

while read site; do
something $site
done <block.list

ssalman
July 21st, 2006, 06:03 PM
Why not a while loop?

while read site; do
something $site
done <block.list

Excellent, it worked, thanks a lot!

BTW -- do you know a good tutorial on bash scripting? and do you know how to break a single script command line into several lines for editing purposes, thanks!

kabus
July 22nd, 2006, 03:31 AM
BTW -- do you know a good tutorial on bash scripting?


These two are essential reading:

http://tldp.org/LDP/Bash-Beginners-Guide/html/index.html
http://tldp.org/LDP/abs/html/index.html


and do you know how to break a single script command line into several lines for editing purposes, thanks!

Add a \ at the point were you want to break up the line.

ssalman
July 22nd, 2006, 03:01 PM
Thanks, kabus for all your help.