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.

Is there any way to create a virtual file, such that reading from the file actually reads from the stdout of a command; writing to the file is acually writing to the stdin of a command?

So far I have kludged this with an inotifywait on a file, which calls a command when the file is modified, taking it's input from the file and writing back to it.

I don't like that the inotifywait has to be constantly restarted though (and I have to ensure that it is always running). I only use this file perhaps twice a week.

share|improve this question
add comment

3 Answers 3

up vote 7 down vote accepted

You may be looking for a named pipe.

mkfifo f
{
  echo 'V cebqhpr bhgchg.'
  sleep 2
  echo 'Urer vf zber bhgchg.'
} >f
rot13 < f

Writing to the pipe doesn't start the listening program. If you want to process input in a loop, you need to keep a listening program running.

while true; do rot13 <f >decoded-output-$(date +%s.%N); done

Note that all data written to the pipe is merged, even if there are multiple processes writing. If multiple processes are reading, only one gets the data. So a pipe may not be suitable for concurrent situations.

A named socket can handle concurrent connections, but this is beyond the capabilities for basic shell scripts.

At the most complex end of the scale are custom filesystems, which lets you design and mount a filesystem where each open, write, etc., triggers a function in a program. The minimum investment is tens of lines of nontrivial coding, for example in Python.

share|improve this answer
    
yeah it's looking like fuse is the way to go, thanks –  trideceth12 Mar 6 '13 at 0:33
add comment

What you describe is essentially a FIFO special file, see fifo(7). They are created by mkfifo(1), and a process can then open them under Linux for R/W (not all Unixy systems handle that). Two independent processes can then use it to communicate.

share|improve this answer
add comment

Some things to be aware of regarding named pipes: A pipe cannot - unlike a file - have multiple readers. Once its content is read it's gone. So you need to loop, i.e. you'll need to constantly push content into the pipe from the writer.

I've been searching for an answer myself to this question and the only other thing I've been able to come up with is scriptfs which is based on fuse.

share|improve this answer
add comment

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.