How to create custom file descriptors on linux besides stdout/stderr?

76 views Asked by At

I want to create a special file descriptor to co-exist along /dev/std{in,out,err}. For example, lets say /dev/fd/3 and /dev/customout are created and all programs and scripts can use it in the same way they might use stderr.

In particular, if I have the following script demo.sh:

#!/usr/bin/env bash

echo hello from fd 3 aka customout >& /dev/customout

echo hello from stdout

Calling like this to silence customout

./demo.sh 3>/dev/null

Should output only stdin data (plus any stderr)

hello from stdout

And calling like this to silence stdin

./demo.sh 1>/dev/null

Should output only customout data (plus any stderr)

hello from fd 3 aka customout

But calling without redirection should show everything:

./demo.sh

Should output stdout, stderr, customout

hello from fd 3 aka customout
hello from stdout

p.s. Maybe customerr is a better name for the concept.

There is this existing question but its not exactly what I am trying to achieve:

How to create a customized file descriptor on linux

1

There are 1 answers

2
stark On

Programs inherit the fds of their parent. If you write a shell that opens 4 fds and start a program with fork/execve then it will inherit 4 fds. See also execve() and sharing file descriptors