stpl.sh

stpl - a simple template system

This library provides functions to generate e.g. configuration files from templates. In the template file values/elements which needs to be substitute are defined by its KEY and enclosed by square brackets [KEY]. Variables, like $TPL_KEY, defined in the shell script are then used to fill-up the template.

Dependencies

N/A

Usage

stpl_fill TPL_FILE [VPREFIX]
Returns the template file (TPL_FILE) filled with variables.
stpl_check TPL_FILE [VPREFIX]
Check if all variables appearing in template file (TPL_FILE) are set.
stpl_show TPL_FILE [VPREFIX]
Returns a table containing the key/value pairs for variables appearing in template file (TPL_FILE).
stpl_data TPL_FILE [VPREFIX]
Returns the variable definitions to write in a file, which can be included by a shell script, or imported with libinclude.

Variables

stpl_VPREFIX
(default: TPL) Default prefix for input variables. Can be individually reset at each function call.
stpl_KEYOPEN and stpl_KEYCLOSE
(default: \\[ and \\]) Opening and closing string for the keys in the template file (TPL_FILE). Note: Special characters needs to be mask two times!
stpl_ACHECK
(default: 0) Do automatic check (1) if all variables used in template file (TPL_FILE) are set and not empty or not (0)?
stpl_HCHECK
(default: 0) Should stpl_check exit (1) or cast only a warning (0) if a variable (used in TPL_FILE) is empty or not set?
stpl_SVAR
(default: 'SW0 SW1 SW2 SW3 SW4 SW5 SW6 SW7 SW8 SW9') List of keys/variables which will be ignored if empty or not set while variables used in TPL_FILE are checked.

Examples

For all examples we refer to the following template file (example.tpl):

# example file for stpl.sh library
#----------------------------------
temperate = [TEMP]
pressure = [PRESS]
type = [TYP]
[SW0]statement-0
[SW1]statement-1

First we include libinclude.sh, import the stpl library and define the variables we wish to fill in the template above:

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

# We define the template file where the variable
# fields (like: [KEY]) are included.
TPL_FILE='example.tpl'

# Here we define the variables VPREFIX_KEY to
# fill the template. The default for VPREFIX is 'TPL'.
TPL_TEMP='200.9'
TPL_PRESS='1.0'
TPL_TYP='NVE'
TPL_SW0='# (to comment out) '
TPL_SW1=''

fill a template

To fill the template you now just need to call:

stpl_fill $TPL_FILE

which returns:

# example file for stpl.sh library
#----------------------------------
temperature = 200.9
pressure = 1.0
type = NVE
# (to comment out) statement-0
statement-1

or you can simply pipe it into a file (outfile) like:

stpl_fill $TPL_FILE > outfile

check variables

You can check if all variables appearing in the template file are set and not empty with:

stpl_check $TPL_FILE

This will return 0 if all variables are defined and not empty. The empty variable $TPL_SW1 will be ignored, because the key SW1 is in $stpl_SVAR, see above.

If we unset a variable:

TPL_TYP=''
stpl_check $TPL_FILE

it will drop a warning (stpl.sh) Warning: Variable (TPL_)TYP is empty or not set!, or if stpl_HCHECK=1 the script will drop an error message and terminate with exit code 1.

show variables

The function call stpl_show $TPL_FILE returns a table containing all keys and values:

key       value
========  ========
TEMP      200.9
PRESS     1.0
TYP       NVE
SW0       # (to comment out)
SW1

write data to file

With the function call

stpl_data $TPL_FILE > example_data.sh

all variables (key/value pairs) appearing in the template file are now written to an include-able file (example_data.sh):

TEMP='200.9'
PRESS='1.0'
TYP='NVE'
SW0='# (to comment out) '
SW1=''

read data from file

Now we can use the import function from libinclude to read this data from file:

import example_data as TPL2
# do some changes
TPL2_SW1='# (now this) '
TPL2_SW0=''
TPL2_TYP='NVT'
# output...
stpl_fill $TPL_FILE TPL2

which will return:

# example file for stpl.sh library
#----------------------------------
temperature = 200.9
pressure = 1.0
type = NVT
statement-0
# (now this) statement-1