22.3.7 Redirecting channels when executing a program

When we execute programs with the functions mbfl_program_exec(), mbfl_program_execbg(), mbfl_program_replace(), and other functions that do not accept channels as arguments, we could perform channels redirection by composing expressions like:

mbfl_program_exec /the/program.exe </some/input >/some/output 2>/some/error

but this way we redirect the channels of the function call, not the channels of the executed subprocess; this may be a problem, for example, when we use the predefined options --verbose, --debug, --show-program that output text on the stderr channel, Predefined options. For this reason there exist the functions mbfl_program_exec2(), mbfl_program_execbg2(), mbfl_program_replace2(), and other functions that do accept channels as arguments.

The functions mbfl_program_exec(), mbfl_program_execbg(), mbfl_program_replace(), and similar should be used only when it is fine, for the executed subprocess, to use the same channels of the invoking parent process (the Bash script itself).

We can always use the default channels when calling mbfl_program_exec2(), mbfl_program_execbg2(), mbfl_program_replace2(), and similar; for example:

mbfl_program_exec2 0 1 2 /bin/ls --long

is equivalent to:

mbfl_program_exec /bin/ls --long

In general we could use both numeric channels and symbolic channels; on a system where symbolic channels are created under /dev, we can compose expressions like:

mbfl_program_exec2 /dev/stdin /dev/stdout /dev/stderr /bin/ls --long
mbfl_program_exec2 /dev/fd/0  /dev/fd/1   /dev/fd/2   /bin/ls --long

but we must remember that the human–readable aliases /dev/stdin, /dev/stdout, /dev/stderr, /dev/fd/0, /dev/fd/1, … are not always available; for example, when a script is run from a cron job: these aliases may not exist.

The solution that always works is to use numeric channels ‘0’, ‘1’, ‘2’, …; but we must beware that using numeric channels above ‘9’ may collide with the channels used internally by Bash itself (as it is stated in the documentation of GNU Bash version 4). So we should consider using the fd module to open channels as we see fit, Handling file descriptors.

Redirecting stderr to stdout

There are programs that output useful informations on their stderr channel (example: the at command). When we execute programs with the functions mbfl_program_exec(), mbfl_program_execbg() or mbfl_program_replace(), there is a simple, special case that is sometimes useful: using the standard channels for input and output; redirecting the standard error channel to the standard output channel.

Function: mbfl_program_set_stderr_to_stdout
Function: mbfl_program_redirect_stderr_to_stdout

Just for the next invocation to mbfl_program_exec() and similar functions: redirect stderr to stdout, that is: use the 2>&1 redirection for the executed program.

Function: mbfl_program_reset_stderr_to_stdout

Reset the stderr to stdout redirection, disabling it.


This document describes version 3.0.0-devel.9 of Marcos Bash Functions Library.