3

What I want is this:

Having a file called file somewhere in the filesystem with a content something like:

#!/bin/whatever-interpreter-you-want
echo 'line1'
echo 'line2'

When ANY (almost any since must exist a way to edit the script itself) program opens file to read it gets:

line1
line2

As its contents instead of:

#!/bin/whatever-interpreter-you-want
echo 'line1'
echo 'line2'

Something like file contents being executed on the fly by the filesystem or the kernel itself (please, no cron, no inotify, no duct tape).

matihus
  • 31
  • 1
    Can you please explain what you need this for? There might be a simpler way to accomplish it. – Joseph R. Aug 12 '15 at 06:12
  • I don't think what you want is possible: A file is either executed or read. – FelixJN Aug 12 '15 at 07:59
  • If you are willing to open a different file, then the different file can be a pipe, and the program would have to be modified to write to the pipe, close the pipe, and loop (in case the file was opened for reading again). – Robert Jacobs Aug 12 '15 at 19:11
  • @JosephR. My question is largely hypothetical, but I can think in something like a system with centralized configuration in Sqlite database and /etc/ full of these scripts querying such database when a daemon (or another program) opens them for reading. I've discarded cron because I want the files contents being generated just before they are opened, and I don't want a daemon regenerating these files from time to time. I don't know if applicable, but I discarded inotify too because I consider it very linux-specific. – matihus Aug 12 '15 at 19:23
  • 1
    Related: http://unix.stackexchange.com/questions/273812/have-a-file-generate-its-content-on-demand. – Golar Ramblar Apr 02 '16 at 17:56

2 Answers2

2

This can certainly be achieved using FUSE. Without exact details of your goals I've provided some details of FUSE below.

steve
  • 21,892
1

You might put your file in some FUSE filesystem. You'll then need to write some FUSE server to handle IO to such files. It might take you some significant effort (so I am not sure it is worth the pain)