开发者

how to map a file to a shell command to be executed when you `cat` that file? [closed]

Closed. This question is off-topic. It is not currently accepting answers.

Want to improve this question? Update the question so it's on-to开发者_开发知识库pic for Stack Overflow.

Closed 11 years ago.

Improve this question

I'd like to create a file and hook it up somehow to a shell command that gets executed whenever I cat that file. So that the content of the file is the stdout of the shell command.

Something like /proc/meminfo.

Thanks


/proc/meminfo lives in a special filesystem, procfs, that is implemented by the kernel. You could use FUSE to do something similar in userspace.


We used to do this "back in the day" to make program generated .finger files.

Make your file into a named pipe. You use the mknod program to do this.

Then make a program, which can be a shell script, that continuously loops and writes content into that named pipe.

#!/bin/sh

mknod file p 2>/dev/null
while true; do
        (
                echo This is my file.
                echo There are many like it.
                echo But this one is mine.
        ) >> file
done

For some reason it won't let me kill that with Control-C, so I have to use Control-Z, then kill %1.

Now when you cat file you will get the contents, like this:

$ cat file 
This is my file.
There are many like it.
But this one is mine.


Perhaps you should turn the problem around and write a shell command and execute that instead of cat to start with? Anything "echo"'ed will appear on stdout. The solution you are proposing is "not the unix way".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜