Tiny module system for POSIX compatible shell script.
Supported shell: dash
, bash
, zsh
, ksh
, mksh
, yash
, posh
, busybox (ash)
SH_MODULE_DIR=<module base directory>
. "$SH_MODULE_DIR/module.sh"
IMPORT namespaceA/moduleA1:a1
a1_hello "moduleA1"
IMPORT namespaceA/moduleA2:a2
a2_hello "moduleA2"
example module directory
Module base directory ($SH_MODULE_DIR)
├ module.sh
│
├ namespaceA
│ ├ moduleA1.sh
│ └ moduleA2
│ └ moduleA2.sh
└ namespaceB
├ moduleB1.sh
└ moduleB2.sh
example module: namesapceA/moduleA1.sh
#!/bin/sh
# initializer: Invoked by module.sh to export functions
namesapceA_moduleA1() {
# localize var for all function
DEFAULT_LOCAL var B1
# export hello function and localize var1
EXPORT hello var1
# Using other module (Do not use IMPORT in module)
DEPENDS namespaceB/moduleB1
}
# Invoked before each function call
# $1 is the name of the function to be called.
namesapceA_moduleA1_prepare() {
var=123
B1=namespaceB_moduleB1
}
# Real name of function to export
_namesapceA_moduleA1_hello() {
var1='this is local variable'
var2='this is not local variable'
echo "hello $@"
}
_namesapceA_moduleA1_bye() {
# call other module function with full module name
namespaceB_moduleB1_bye "$@"
# In this way you can use short aliases.
${B1}_bye "$@"
}
Module base directories separated by :
Use to import modules.
Usage: IMPORT <namespace/module>[:<prefix>] [<funcname[:alias]>...]
namespace/module exports three functions, foo(), bar() and baz().
IMPORT namespace/module
- Imports all functions from module with default prefix.
- namespace_module_foo(), namespace_module_bar(), namespace_module_baz() functions are defined.
IMPORT namespace/module:
- Imports all functions from module without prefix.
- foo(), bar(), baz() functions are defined.
IMPORT namespace/module:sh
- Imports all functions from module with prefix.
- sh_foo(), sh_bar(), sh_baz() functions are defined.
IMPORT namespace/module foo
- Imports specified functions from module with default prefix.
- namespace_module_foo() function is defined.
IMPORT namespace/module: foo
- Imports specified functions from module without prefix.
- foo() function is defined.
IMPORT namespace/module:sh foo
- Imports specified functions from module with prefix.
- sh_foo() function is defined.
IMPORT namespace/sample:sh foo bar:my_bar baz:baz
- Imports specified functions from module with alias.
- sh_foo(), my_bar(), namespace_baz() functions are defined.
localize variable for all function.
Use to export function in module initializer.
Usage: EXPORT <funcname>[:<original>] [variable-names...]
The scope of specified variable-names and IFS will be local.
Using other module function.
Usage: DEPENDS <module>...
- First version.
- Add DEPENDS function.
- Add MODULE_SOURCE MODULE_NAME.
- Fix several bugs.
- Fix several bugs.
- Change specification when prefix omitted.
- Allow to place module under the directory.
- Add DEFAULT_LOCAL function.
- Add prepare callback.
- EXPORT can specify the function name of the original.
- Use shellspec for testing.
- Fixed for unexpected modify path problem in zsh.