0

In Linux, I would like to create a file where the contents of the file are dynamically generated by the output of a program.

The examples which already exist are /dev/random /dev/urandom, /dev/zero, etc...

I would like to create a file such as /home/sam/currentdate.txt where by the contents of the file are the result of running a command (such as date, and would read:

[sam@myserver ~]$ cat ./currentdate.txt
Wed Dec  2 12:12:09 PST 2015
[sam@myserver ~]$ 

For my use case, I need to track the output of certain programs by another program which can not read the contents of a program's output, but rather the contents of a file. I can save the contents of the output via a script by using date > filename.txt but that requires saving the output to disk first, which is going to be inefficient in my use case.

user1955162
  • 296
  • 3
  • 16

2 Answers2

2

A named pipe may be your friend. Create a named pipe and have your process(es) write to it. Your reading process can then read from it.

user9517
  • 115,471
  • 20
  • 215
  • 297
1

This looks like an instance of the XY problem; what exactly are you trying to accomplish?

If you want to read the output of a program and use it in your script, you can call the target program and assign its output to a variable: https://stackoverflow.com/questions/4651437/how-to-set-a-bash-variable-equal-to-the-output-from-a-command.

If instead you actually want the program to run asynchronously and store its output where your script can read it, you can either use a disk file (which really doesn't have as much overhead as you think), or a named pipe, as Iain suggested.

Massimo
  • 70,200
  • 57
  • 200
  • 323
  • I am trying to write zfssend incremental snapshots to tape using a backup software that will only track changes to files. I don't want to write down the delta incremental snapshots to tape, so I would like to create a file that would contain the command which reads out the delta file itself. Thats my use case. I will investigate the named-pipe solution recommended by lain. – user1955162 Dec 02 '15 at 22:37