Previous: , Up: program   [Contents][Index]


11.4 Declaring the intention to use a program

To make a script model simpler, we assume that the unavailability of a program at the time of its execution is a fatal error. So if we need to execute a program and the executable is not there, the script must be aborted on the spot.

Functions are provided to test the availability of a program, so we can try to locate an alternative or terminate the process under the script control. On a system where executables may vanish from one moment to another, no matter how we test a program’s existence, there’s always the possibility that the program is not “there” when we invoke it.

If we just use mbfl_program_exec() to invoke an external program, the function will try and fail if the executable is unavailable: the return code will be false.

The vanishing of a program is a rare event: if it’s there when we look for it, probably it will be there also a few moments later when we invoke it. For this reason, MBFL proposes a set of functions with which we can declare the intention of a script to use a set of programs.

A command line option is predefined to let the user test the availability of all the declared programs before invoking the script. Predefined options.

Function: mbfl_declare_program program

Register program as the name of a program required by the script; mbfl_program_find() is used to locate the program on the system. Checking programs existence.

If program is a file name with no directory part (examples: sed, grep) the selected program is the full pathname of the file in one of the directories of PATH.

If program is a relative pathname (examples: ../bin/sed, ./grep): the selected program is the full pathname of the file normalised by this function with respect to the current working directory (with a call to mbfl_file_normalise()).

The return value is always zero.

Function: mbfl_program_validate_declared

Validate the existence of all the declared programs. The return value is zero if all the programs are found, one otherwise.

This function is invoked by mbfl_getopts_parse() when the --validate-programs option is used on the command line.

It may be a good idea to invoke this function at the beginning of a script, just before starting to do stuff, example:

mbfl_program_validate_declared || \
   exit_because_program_not_found

If verbose messages are enabled: a brief summary is echoed to stderr; from the command line the option --verbose must be used before --validate-programs.

Function: mbfl_program_found program
Function: mbfl_program_found_var _RV program

Print the pathname of the previously declared program. Return zero if the program was found, otherwise print an error message and exit the current (sub)shell by invoking exit_because_program_not_found().

This function should be used to retrieve the pathname of the program to be used as first argument to mbfl_program_exec():

The function variant _var will store the result in _RV rather than printing it.

function program_wrapper () {
    mbfl_mandatory_parameter(ARGUMENT, 1, argument)
    shift
    local PROGNAME FLAGS

    mbfl_program_found_var PROGNAME myprog || exit $?

    mbfl_option_verbose_program && FLAGS+=' --verbose'
    mbfl_program_exec "$PROGNAME" $FLAGS "$ARGUMENT" "$@"
}

Remember that we cannot use:

local PROGNAME=$(mbfl_program_found 'myprog') || exit $?

because local has exit status zero even if mbfl_program_found() fails, so the error will not be reported.

Function: exit_because_program_not_found
Function: return_because_program_not_found

Exit or return with code 99.


Previous: , Up: program   [Contents][Index]

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