22.3.3 Core API for program execution with replacement

Function: mbfl_program_replace PROGRAM ARG

Like mbfl_program_exec(), but execute the program through the Bash command exec, so replacing the current process without creating a new one.

If the function mbfl_program_set_exec_flags() was used to select arguments for the Bash built–in exec: that request is honoured by putting the selected arguments on the command line of exec. The request is considered “consumed” and reset.

If a trap is set for the ‘EXIT’ event: such trap is executed by Bash, See trap. This means the MBFL’s atexit handlers are executed, Running scripts at exit–time.

Inside a script, we can easily test program execution with replacement as follows:

{
  (mbfl_program_replace "$mbfl_PROGRAM_BASH" '-c' 'exit 123';)
  $?      ⇒ 123
}

{
  local RESULT

  RESULT=$(mbfl_program_replace "$mbfl_PROGRAM_BASH" '-c' 'echo password';)
  "$RESULT"       ⇒ password
}

and we can select flags for exec as follows:

local RESULT

RESULT=$(mbfl_program_set_exec_flags '-a' 'secret'
         mbfl_program_replace "$mbfl_PROGRAM_BASH" '-c' 'echo $0';)
"$RESULT"       ⇒ secret

If the redirection of stderr to stdout is requested with mbfl_program_redirect_stderr_to_stdout(): such request is honoured and consumed. For a discussion of channels redirection when calling this function, see Redirecting channels when executing a program.

Function: mbfl_program_replace2 INFD OUFD ERFD PROGRAM ARG

Like mbfl_program_replace(), but redirect the standard input, output and error channels using the given channels.

If the redirection of stderr to stdout is requested with mbfl_program_redirect_stderr_to_stdout(): such request is ignored and consumed. For a discussion of channels redirection when calling this function, see Redirecting channels when executing a program.

Function: mbfl_program_set_exec_flags FLAG

Store the strings FLAG as internal state. At the next program execution with one of the functions mbfl_program_replace*(): the string is consumed as flags for the Bash built–in exec.

Function: mbfl_program_reset_exec_flags

Reset to empty the internal state representing flags for the Bash built–in exec.


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