How to include all files from a directory in shell script (/etc/init.d/iptables in this case)

Add the following line to your init.d script.

run-parts --report /etc/iptables/include.d

It will run everything in the directory as a shell script (need to be executable).

If you you only want to execute files that ends with .port you could use something like:

run-parts --regex '\.port$' /etc/iptables/include.d/

If you want to make sure the order is correct you can name the files:

10_web.port
20_ssh.port
etc..

for f in /etc/iptables/include.d/*
 . $f
done

note space between dot and %f

Saurabh is right - this will not necessary work as you intend, but use some naming convention eg 10-xxx, 20-yyy and so on and it might be manageable.


You can define simple function in bash:

function include() {
    for FILE in $( find "$1" -type f -print | sort )
    do
        source $FILE
    done
}

and then:

include some_dir/*

or even:

include some_dir/*.conf