0

This question already has an answer here:

I have this idea and maybe it is not feasible, but I think it worth asking.

Let's say a user is running this command:

cat ~/file.txt

Whenever something tries to read from this file, I would like to run a script or command in the background instead and return the response of that script as content.

Somehow like doing:

ln -s /folder /symlink

But for scripts (idea of what I have in mind):

symlinkcommand /filepath/file.txt "command to run"

Hope I made myself clear. Please reply with your ideas and suggestions if any.

Thanks.

marked as duplicate by Gilles 'SO- stop being evil', Jeff Schaller, PersianGulf, slm May 3 '16 at 4:35

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    you are trying to create a symlink to a real file and want to run a different command, if someone tries to access this symlink'ed file and feed the different command's output as the output for the command user tried to run. Is this right ? Like cat /etc/passwd will return the passwd file but the password hashes scrambled or cleared ? If you can tell what you are trying to accomplish instead of beating around the bush, you may get better suggestions. – MelBurslan May 2 '16 at 13:56
2

What you're looking for is possible, but perhaps not exactly as you envision. The way I have seen it done most often (and it is admittedly a very rare occurrence) is to create the file being read a named pipe (aka FIFO) special file, using the mknod command:

mknod file.txt p

You would then need to start the script you want to use to generate the "file contents" in a separate shell, and have it restart automatically when it completes in order to allow for multiple reads of that special file. The STDOUT of the script would be redirected to the named pipe, and the script would pause until some other process - the original user's process - started to read the pipe somehow. Once the script ended, the "EOF" signal would propagate to the user's process, making everything look normal.

0

Simplest way for you is to use bash alias, as example
In case you talking about user command input.

Another way to do that, is filesystem in user space FUSE

Python implementation of fuse basic filesystem - https://www.stavros.io/posts/python-fuse-filesystem/

Unfortunately list of fuse filesystems is broken(was on FUSE sourceforge, they moved, list is't), and who knows, maybe already done.

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