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

607

Solution 1

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..

Solution 2

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.

Solution 3

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

Solution 4

You may also consider building the iptables script from template files, one of which would be the original iptables script. create a script which will read your template files in the relevant directories and create a new iptables script from them. That way when you need to make changes you do so in the templates and just rerun your script generator.

Using this method you could even get fancy and place markers in the base template which can be used to signal when to include files from specific directories in your template tree.

Share:
607

Related videos on Youtube

P i
Author by

P i

Updated on September 17, 2022

Comments

  • P i
    P i over 1 year

    I am implementing an audio passthrough using the RemoteIO audio unit, by attaching a render callback to the input scope of the output bus (ie the speakers).

    Everything works swimmingly, ...

    OSStatus RenderTone(
                        void *                      inRefCon, 
                        AudioUnitRenderActionFlags  * ioActionFlags, 
                        const AudioTimeStamp        * inTimeStamp, 
                        UInt32                      inBusNumber, 
                        UInt32                      inNumberFrames, 
                        AudioBufferList             * ioData)
    
    {   
        SoundEngine * soundEngine = (SoundEngine *) inRefCon;
    
        // grab data from the MIC (ie fill up ioData)
        AudioUnit thisUnit = soundEngine->remoteIOUnit->audioUnit;
        OSStatus err = AudioUnitRender(thisUnit, 
                                       ioActionFlags, 
                                       inTimeStamp, 
                                       1, 
                                       inNumberFrames, 
                                       ioData);
        if (result)
        {
            printf("Error pulling mic data");
        }
    
        assert(ioData->mNumberBuffers > 0);
    
        // only need the first buffer
        const int channel = 0;
        Float32 * buff = (Float32 *) ioData->mBuffers[channel].mData;
    }
    

    until I add that last line.

        Float32 * buff = (Float32 *) ioData->mBuffers[channel].mData;
    

    With this line in place, no errors, simply silence. Without it, I can click my fingers in front of the microphone and hear it in my headset.

    EDIT: AudioBuffer buf0 = ioData->mBuffers[0]; // Is sufficient to cause failure

    What is going on?

    It is not an error caused by the compiler optimising out an unused variable. If I set buff++; on the next line, behaviour is the same. although maybe the compiler can still detect the variable is effectively unused.

    • P i
      P i over 13 years
      AudioBuffer buf0 = ioData->mBuffers[0]; // Is sufficient to cause failure
  • user12096
    user12096 almost 15 years
    this is a good point. thank you for pointing this out. i am updating the question to use several include.d directories, so I can give them an order. I have many services that do not depend on each other and the setup would still make sense in these cases.
  • user12096
    user12096 almost 15 years
    i think this is great, because i do not rely on an external command (run-parts)
  • user12096
    user12096 almost 15 years
    this is even better than pQd's solution.
  • user12096
    user12096 almost 15 years
    will the order of the files when numbered 10-xxx, 20-yyy be respected automatically by the for f in ?
  • pQd
    pQd almost 15 years
    @matnagel - should be. if you have any doubts, replace /etc/iptables/include.d/* with ls -1 /etc/iptables/include.d/*.extension - with ls you can control sorting criteria, default one is by name. notice special quote symbol.