General Usage

libinclude.sh allows to include/import functions and or variables from external FILEs in a Bourne shell script. To utilize this, you have first to include the script itself. It makes sense to include it right in the header, like:

#!/bin/sh
. libinclude.sh

Now the following commands are available to handle the include/import of files:

include FILE
Functions and variables are now accessible under the same name then used in FILE.
import FILE
Functions (FUNC) and variables (VAR) in FILE are now available under the name FILE_FUNC respectively FILE_VAR.
import FILE as ALIAS
Functions (FUNC) and variables (VAR) in FILE are now available under the name ALIAS_FUNC respectively ALIAS_VAR.
from FILE import FUNC
Only the function FUNC from FILE will be imported and available under the same name then used in FILE.

Note

You can omit the extention ‘.sh’ on FILE. So you can write ‘mylib’ instead of ‘mylib.sh’. libinclude will search for ‘FILE’ and ‘FILE.sh’.

Note

Check the Search Path so know where libinclude will search for FILE.

Note

The library features, e.g. dependency check, will always apply regardless which include/import function is used!

Examples

For all examples we refer to the following library (mylib.sh):

# lib variable
VAR='abc'

# lib function
FUNC () {
        ...
}

include

The library is simply included after preprocessing and dependency check.

#!/bin/sh
. libinclude.sh
include mylib
echo $VAR
FUNC

Functions and variables are accessible under the same name then used in the library file mylib.sh.

import

Functions (FUNC) and variables (VAR) in mylib.sh are now available under the name mylib_FUNC respectively mylib_VAR.

#!/bin/sh
. libinclude.sh
include mylib
echo $mylib_VAR
mylib_FUNC

import as

Functions (FUNC) and variables (VAR) in mylib.sh are now available under the name ALIAS_FUNC respectively ALIAS_VAR.

#!/bin/sh
. libinclude.sh
include mylib as ALIAS
echo $ALIAS_VAR
ALIAS_FUNC

from FILE import FUNC

Only the function FUNC from mylib.sh will be imported and available under the same name then used in the library file mylib.sh.

#!/bin/sh
. libinclude.sh
from mylib import FUNC
FUNC

Search Path

The standard search pathes for FILE is:

<SCRIPT_DIR>
<SCRIPT_DIR>/bin
<SCRIPT_DIR>/lib
.
./bin
./lib
$HOME/bin
$HOME/lib
/usr/lib/libinclude.sh (/usr/local/lib/libinclude.sh on BSD systems)
$PATH

<SCRIPT_DIR> is the directory where the script that uses libinclude.sh resides.

You can extend the serach path by modifying the variable $SH_INCLUDE_PATH:

#!/bin/sh
. libinclude.sh

SH_INCLUDE_PATH="$SH_INCLUDE_PATH:$HOME/shlib"

or by setting a global variable in the configuration file of the shell (.shrc):

export SH_INCLUDE_PATH="$HOME/shlib"

Note

On many Linux systems sh is associated with bash, here you need to add the line to .bashrc instead of .shrc!

Debugging

At the moment libinclude.sh offers a very basic debugging mode.

If the variable INCLUDE_DEBUG is set the processed file, containing all code that will be included, will be displayed in the $PAGER just before inclusion.

You can either set it in your script:

#!/bin/sh
. libinclude.sh

INCLUDE_DEBUG=true

or exporting the variable:

export INCLUDE_DEBUG=true # bash or sh

or

setenv INCLUDE_DEBUG true # (t)csh