0

I have a would-be POSIX script that includes filtering the stderr output:

exec <cmd> "$@" 2> >(grep -v "blih bluh blah")

Redirection does not occur because process substitution is undefined (viz [SC3001]). I played with file descriptors and looked at this post as well as at others, but I am not sure about the implications of:

exec <cmd> "$@" 2>/dev/fd/5; exec grep -v "blih bluh blah" 5<&- >&2

or perhaps

exec <cmd> "$@" 2>/dev/fd/5 && grep -v "blih bluh blah" 5<&- >&2

Critical eye(s) would be welcome.

EDIT: what I intend should probably be closer to:

exec {<cmd> "$@"; grep -v "blah" /dev/fd/5 5<&- >&2} 5<&2

although I can sense at least 2 issues here, with: (i) grep being executed asynchronously, as it is instructed behind a ";", and (ii) the way redirection of stderr as specified affects the current shell execution environment.

In addition in the above expression exec is followed by a command, so the shell is replaced with <cmd> without creating a new process. The POSIX man pages specifies that if arguments are specified, they are interpreted as arguments to <cmd>. But is that limited to "$@" here ?

And how does exec deal with exec {<cmd1>; <cmd2>} 5<&2 ?

Cbhihe
  • 2,701
  • 1
    Wouldn't what you propose block if the output is larger than the pipe buffer? – Kusalananda Jan 28 '23 at 20:07
  • @Kusalananda: Dead on ! As it turns out the output to stderr is quite large (quite a bit more than 5000 lines of text). As this line of code is executed early on when the window manager or server X is launched in a machine w/ GUI, it does block, thus preventing the host from booting (I just tested that :-/ ). What would a pipe streaming option look like (the point being that everything that is not filtered out should continue to go to stderr) ?? – Cbhihe Jan 28 '23 at 20:51
  • I'd expect exec somecmd 2>/dev/fd/5; to just throw an error, since fd 5 is likely not open at that point. Plus if it did work, it'd replace the shell, and the next command wouldn't run. You need to start the grep in the background first. – ilkkachu Jan 28 '23 at 21:17
  • @ilkkachu: Silly me, what I intend should probably be closer to: exec {<cmd> "$@"; grep -v "blah" /dev/fd/5 5<&- >&2} 5<&2. Could starting grep in background be avoided that way ? I think there still should be at least 2 problems: (i) grep being executed asynchronously (as it is instructed behind a ";") and (ii) with the way redirection of stderras specified affects the current shell execution environment. OP is written too vaguely perhaps. – Cbhihe Jan 30 '23 at 09:28

0 Answers0