Ubuntu – How to create a device like /dev/zero, /dev/random to echo a specific value in demand

devicesfilesystem

I wish to have a customized device to echo a specific value like /dev/zero or /dev/random. But /dev/zero always outputs 0, /dev/random outputs a random number.

The specific value may be configured, for example — 0x1234, 0x4321, 0xF6, or some ASCII streams Yes and No.

How do I create such a device?

Best Answer

Quoting from How is /dev/zero created and how can I make variants like /dev/one? (on SuperUser):

There are device drivers that implement interaction with actual devices, and there are device drivers that just react to read-write requests in useful ways. If you want to you can sit down and write a driver that returns the lyrics of the Star Spangeled Banner. But it's a matter of coding ...

Unless you really need a device file, just use the yes command, which repeats its arguments ad infinitum (or y, if no argument is given):

$ yes Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
...

Also see: