Shell Script Loader


Shell Script Loader is a framework (in a helper script) that can be used by shell script based applications for easy including or loading of co-shell-scripts or subscripts. With this utility scripters may now be able to apply multi-file or module-oriented scripting or programming in shells just like the way they are always done in known languages like Perl, PHP and Ruby. There are many benefits with using module-oriented programming like code sharing, portability, modularity, easy revisioning and restructuring, and reusability.



Design and Coding

The tool has been designed with simplicity, consistency, effectivity and efficiency in mind (both for runtime and coding). Its features were carefully chosen so that they will neither be more nor less than required.

When its scripts were still being made, it was always made sure that the codes and logic flow are always in uniform. Strict coding design is always followed. When a part of the code is modified, it is made sure that not other part use methods that act in a different manner even if both make outputs that are just the same; thereby deciding the most optimum and effective solution that an implementation script can make out of the features that the target shell can provide.



Versions

Shell Script Loader has two supported versions: 0 (base) and 0X (extended). The base version includes the most basic functions that may be required for implementing modular scripting. The extended version contains all the functions of the base version with added extended functions. The extended functions are extended versions of the loader functions that are capable of loading more than one file per single call through glob patterns or regular expressions.



Supported Shells

Full (optimized and tested) support is already available for many shells which includes the major shells bash, ksh, pd ksh and zsh provided with optimized scripts for each one of them; and other shells that are generically compatible with the original sh like ash, dash, heirloom sh, etc., through a universal script that automatically optimizes its functions depending on the features the shell can provide. The universal script is also optimized for bash, ksh and zsh so one may also consider it as just a general optimized script for the whole family shells that are based from sh.



Provided Functions

The basic functions that Loader provides include load(), include() and call(). These functions are available both in the Base and Extended versions. The Extended version includes additional functions that accepts patterns and regular expressions. Those functions are loadx(), includex(), and callx(). All loader functions can accept extra arguments that become the positional parameters when the referred script is loaded.


load() -

Unconditionally loads a file even if it's already been flagged.

usage: load <complete|partial path> [<args>]

include() -

Also loads and flags a file like load() but only if the file has not yet been included or flagged.

usage: include <complete|partial path> [<args>]

call() -

This function acts the same as load() but it loads the file in a separated environment (subshell). This is useful if a user wants to make a co-shell-script do its job without affecting the environment of the caller. The function also returns the exit code of the subshell after the subshell finishes executing. Also loads and flags a file like load() but only if the file has not yet been included or flagged.

usage: call <complete|partial path> [<args>]

loader_addpath() -

Accepts one or more arguments of directory paths and adds them to the list of searchable directories that will be used by load(), include() and call() when searching for files that are specified through partial paths.

usage: loader_addpath <absolute|relative path> ...

loader_flag() -

Marks the specified path to a file as if it's already been loaded with load(), include() or call(). Note that the file referred by path does not need to exist.

This function is mostly useful like when you want to mark a loaded script that has not been loaded by any of the loading functions like the startup or main script that loads the implementation script.

This function can accept any form of path (may it be absolute or relative) but note that the function always convert a path to its clean absolute form.

usage: loader_flag <path> <args>

loader_reset() -

This function acts depending on the argument that is passed to it.

usage: loader_reset [<flags|paths>]


flags-clear the flags of the loaded files
paths-clear the list of search paths

The function clears both if no argument is passed.

loader_finish() -

This will unload loader from shellspace. The function requires no argument.



Extended Functions

In the Extended version, all script manipulating functions (load(), include() and call()) have an extended version. These extended functions can accept arguments that target multiple files through glob patterns or regular expressions.

The functions are usable in a very common way and it is either in the following forms:

(a)

loadx|includex|callx <plainpath> <args>

If a plain path is entered, the function will act just like its non-extended version. Plain paths do not contan '?' and '*'.

(b)

loadx|includex|callx <[prefixpath/]globpattern> <args>

If the expression contains the glob characters '?' and/or '*', it will be considered as a glob pattern.

Bracket characters '[' and ']' are also glob characters but are not easy to tell if those are quoted or not in scripts and may sometimes just cause confusion since brackets may also represent valid characters in a file's name. '?' and '*' are an exemption since they can never be a valid part of a filename and may only represent an invalid filename if quoted.

If it is implied that '[' or ']' is a glob character, we can just use the '-name' or the '-iname' option.

(c)

loadx|includex|callx

 <-name|-iname|-regex|-iregex>

<[prefixpath/]pattern|expression> <args>

If the first argument is one of the options shown, the function will interpret the next string as a pattern or expression according to the option.

-name - argument is a glob pattern
-iname - argument is a glob pattern and is not case sensitive
-regex - argument is a regular expression
-iregex - argument is a regular expression and is not case sensitive



Example Usage

start.sh

This is an optional starter script. Placing the startup methods here is just a convenience and can be placed in the main script instead. This script is also not needed if the scripts are to be compiled.

#!/bin/sh

# load loader.sh
. loader.sh

# include directories to search path
loader_addpath /usr/lib/sh deps source

# load main script
load main.sh

main.sh

include a.sh
include b.sh

echo '---- main.sh ----'

# remove loader from shellspace since
# we no longer need it
loader_finish

# main procedures go from here

# ...

a.sh

include main.sh
include a.sh
include b.sh

echo '---- a.sh ----'

b.sh

include main.sh
include a.sh
include b.sh

echo '---- b.sh ----'

expected output:

---- b.sh ----
---- a.sh ----
---- main.sh ----
(EOF)

extended version examples:

loadx plain.sh
loadx '*.sh'
includex 'source/*.bash'
includex '/usr/share/scripts/bash/*.bash'
callx -iname 'objects/*\.sh'
callx -regex 'modules/.*\.sh'


External Features - Compilers

Shell Script Loader has also been designed so that applications that were built with it can be compiled by an external compiler to form a single file out of the many files that compose them. Currently a line-based compiler that's written in awk is already available. The compiler still has difficult limitations with imitating functions of call() but in common usage it should be more than enough. There's also a plan to make a compiler in a different language or parser like Perl and Parrot but it requres much preparation. For now the current compiler based from awk is already enough in most cases. The compiler can also be downloaded in SourceForge.


Example command using the compiler:


gawk -f loader/compiler.gawk -- \
-a ./source -s /bin/bash \
-H playshell.header -O -o playshell \
source/playshell.sh


Downloads

All project files including compilers may be downloaded here.



Tracker

Any feedback with respect to the project is always apprecated. You may use the project's tracker in SourceForge if you like post a comment or suggestion, post a patch, or file a bug.



Links

Summary Page -

Shell Script Loader's summary page in SourceForge.

SourceForge -

a good place to develop open software.

PlayShell -

an example project that uses Shell Script Loader. Playshell is an another way to play and manage many media files in console or terminal with a shell-like interface.

Bash -

the popular shell that's available in most linux distros with many useful and easy to use features.

Zsh -

a powerful shell with many hacky features that's also designed for interactive use.






Valid XHTML 1.1
Shell Script Loader @ SourceForge.net
free counters