Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I need to maintain a set of plain text files saved in a directory which all have a section taken from another file. An example follows:

Files:

/directory/textfile1 (every other file in the directory needs to "include" the
                      full contents of this file, each file can do so at a
                      different starting position; this file changes often      )
/directory/textfile2 ("includes" textfile1 after a few lines from the top       )
/directory/textfile3 ("includes" textfile1 as a header                          )
/directory/textfile4 ("includes" textfile1 as a footer                          )

Upon changing textfile1, I would like filesystem reads hitting textfile{2..4} to return updated versions with the new content from textfile1.

On web servers, this would be solved with the traditional server side include directives. Now, how would I do that without running a front-end service?

Could I create a symlink that, rather than pointing to another inode, runs some custom code and presents stdout as the contents of the file, somehow like defining custom "reparse points"?

The hypotetical "symlink" would, for instance, run this for file textfile3:

cat /directory/textfile1 /directory/textfile3_static

and thus simulate the existence of a static file whose content depends on textfile1.

I am looking for a solution that uses the least amount of customisation, not writing a custom FUSE module for a special mount point or anything like that.

I may be thinking in the wrong direction here, perhaps it would be easier to trigger an update script replacing placeholders at every modification of textfile1? How could one trap that event without polling for it?

Here some sample text files:

Contents:

/directory/textfile1:
--------------------------------------------------------------------------------
Ferma non stai sei come un ciclone
e non dormi mai
--------------------------------------------------------------------------------

/directory/textfile2:
--------------------------------------------------------------------------------
Juny tu hai un cervellone
quasi tutto sai
Ma quando fai un'invenzione
sono sempre guai
Ferma non stai sei come un ciclone
e non dormi mai
Il centauro Nico ami già
ma lui forse non lo sa
--------------------------------------------------------------------------------

/directory/textfile3:
--------------------------------------------------------------------------------
Ferma non stai sei come un ciclone
e non dormi mai
Il centauro Nico ami già
ma lui forse non lo sa

Come sempre insisterai
allora che pasticci tu di nuovo farai
--------------------------------------------------------------------------------

... etcetera. :)

share|improve this question
    
Would it be acceptable for you to use read-wrapper-scripts? –  rozcietrzewiacz Aug 28 '11 at 19:16
    
@rozcietrzewiacz: read-wrapper-scripts? As in? Could you provide an example or an elucidatory link, please? –  Robottinosino Aug 28 '11 at 21:03
    
I meant scripts that you'd execute instead of reading each file. But I see the answers about named pipes achieve this in a better way. –  rozcietrzewiacz Aug 28 '11 at 21:08

2 Answers 2

up vote 2 down vote accepted

Translating somehow the @polynomial answer to bash:

#!/bin/bash

if [[ ! -e textfile2 ]]; then
  mkfifo textfile2
fi

while true; do
  (
    exec >textfile2
    cat textfile2-header
    cat textfile1
    cat textfile2-footer
  )
done
share|improve this answer

You could do this with named pipes. Basically you would maintain your text files somewhere as 'templates' and a script like this perl example:

http://perl.active-venture.com/pod/perlipc-pipes.html

Would open 'textfile3' as a pipe and when it got a read request it would dump the contents that textfile3 should contain (perhaps by reading textfile3.template and replacing the contents as needed). This would allow you to dynamically change the contents without having to update multiple files and with minimal configuration.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.