libinclude released!

libinclude logo 1If you are working frequently on the command shell of POSIX systems, you may also make use of shell scripts in a more sophisticated manner than just executing a pile of commands. At this point I came to the conclusion, that one can save up  a lot of stupid copy & paste work by transferring often used functions to a separate file which can be simply included if necessary.

I use shell scripts extensively in order to create, execute and finally analyse computer simulations in a highly automatize way. Here I can often re-use functions, like those which are extracting data from output files, e.g.. To make the usage of those libraries more comfortable, Sebastian and me have developed libinclude.

Basics

To put it simply, libinclude is a shell library to import other shell scripts as libraries.

As a library we consider a simple shell script containing function and/or variable definitions. Libinclude offers you the possibility to use those kind of libraries in a comfortable manner. The usage is very close to the syntax of python’s import statement. Functions (FUNC) and variables (VAR) in a library (MYLIB) can be simply included, but also imported in their own namespace (MYLIB_VAR, MYLIB_FUNC). It also provides the import MYLIB as ALIAS and the from MYLIB import FUNC statement, like in python. More about general usage…

Here a simple example of a library file (mylib.sh):

# lib variables
MCOUNT='counting'
MDONE='done'

# lib function
loop () {
    local i
    i=0
    echo -n "$MCOUNT:"
    while [ $i -le $1 ]; do
        echo -n "$i,"
        i=$(($i+1))
    done
    echo "$MDONE!"
} 

and a shell script where it is imported:

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

# counting...
mylib_loop 10

# and 'en francaise'...
mylib_MCOUNT='compter'
mylib_MDONE='fini'
mylib_loop 3

which will return:

 counting:0,1,2,3,4,5,6,7,8,9,10,done!
compter:0,1,2,3,fini!

Features

To ensure the usability of the libraries across different OS and/or software configurations, libinclude provides some features to be utilized in the libraries. One can define dependencies with 3ed party programs, which will be checked while importing and returns a meaningful error message if not met. To adopt the library to different environments, conditional comments provide the possibility to include code blocks depending on defined conditions. It is also possible to run code on file inclusion by define a preprocessing block. More about library features…

Sources

The project is hosted  on SourceForge, and the documentation is available on libinclude.de. For bug reports, suggestions for improvement, or to send us your libraries (preferable with documentation), just mail to info(at)libinclude(dot)de.

Leave a Reply