6.2 Special location handlers

Function: mbfl_location_enable_cleanup_atexit
Function: mbfl_location_disable_cleanup_atexit

Enable or disable running the location handlers through an atexit handler, Running scripts at exit–time. The enable operation registers an atexit handler that cleans up the current stack of locations.

It is suggested to always add the following code to the preamble of a script:

mbfl_atexit_enable
mbfl_location_enable_cleanup_atexit
Function: mbfl_location_handler_suspend_testing

Suspend the test mode inside a location block, by executing the following code:

mbfl_option_test_save
mbfl_location_handler mbfl_option_test_restore

For a discussion of its usage see Testing a script and running programs.

Function: mbfl_location_handler_change_directory NEWPWD

Do this:

  1. Check that NEWPWD is a string representing the file system pathname of an existent and executable directory.
  2. Push the current working directory on a internal directories stack.
  3. Change the current working directory to NEWPWD using mbfl_change_directory().
  4. Register a new location handler that pops the top from the directories stack.

When successful return true, otherwise return false. When verbose mode is enabled: print messages describing the entering and leaving of directories.

Function: mbfl_location_handler_restore_lastpipe

When called, save the state of the lastpipe option of shopt using:

shopt -p lastpipe

Upon exiting the location: the handler restores the previous status of lastpipe. See The Shopt Builtin.

Using this handler is helpful when the last command of a pipe is executed in the current shell, rather than a subshell, to store the result of a computation in a shell variable.

declare RESULT

mbfl_location_enter
{
    mbfl_location_handler_restore_lastpipe
    shopt -s lastpipe
    output_something | while read
    do RESULT+=$REPLY
    done
}
mbfl_location_leave

# use $RESULT
Function: mbfl_location_handler_restore_nullglob

When called, save the state of the nullglob option of shopt using:

shopt -p nullglob

Upon exiting the location: the handler restores the previous status of nullglob. See The Shopt Builtin.

mbfl_location_enter
{
    mbfl_location_handler_restore_nullglob
    shopt -s nullglob

    declare MATCH

    # The loop never executes its body if there are no files
    # matching the pattern.
    for MATCH in /path/to/*.tex
    do # do something
    done
}
mbfl_location_leave

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