The module
function of the Environment Modules1 package does its work by modifying various environment variables of the current shell process.
Unfortunately, this function returns 0 whether it succeeds or not2, which makes it difficult for a client script to respond appropriately to failures.
I would like to implement a function wrapper mymodule
around module
that passes all its arguments straight through to module
, and properly returns a non-zero value if module
fails.
The only way that mymodule
can detect whether module
failed is to inspect the output module
function writes to stderr, if any.
The problem is that I can't come up with a reasonable way for mymodule
to get this output without nullifying module
's actions. More specifically, almost all the ways I can think of to capture module
's stderr into a variable entail running module
in a child process, thus preventing it from doing its job (which requires modifying the current shell).
The one exception to the above would be to redirect module
's stderr to a temporary file, but I hate the idea of creating a file every time the module
function runs.
Is there some way for mymodule
to invoke module
in the current environment and at the same time capture its stderr in a variable?
I am interested in answers for both zsh
and bash
.
1 Not to be confused with the Lmod environment modules package, which has a very similar interface.
2 At least this is the case for the ancient version 3.2.9 that I must work with. I have no control over this.
<<
) or here-strings (<<<
). Anyway here is an example of trouble free use of tempfiles:t=$(mktemp); exec 7<"$t" 8>"$t"; rm "$t"; ...; module 2>&8; case $(cat <&7) in ... esac
– Jun 16 '19 at 04:13