bash-utils/shtpl
dig e513897de7 [shtpl] Added utility functions 2019-07-26 13:03:48 +02:00
..
exemple Added shtpl installer, path variables 2019-03-02 21:12:33 +01:00
tests Added shtpl installer, path variables 2019-03-02 21:12:33 +01:00
README.md [shtpl] Added utility functions 2019-07-26 13:03:48 +02:00
install Upgrade shtpl installer 2019-03-02 22:22:04 +01:00
shtpl [shtpl] Added utility functions 2019-07-26 13:03:48 +02:00

README.md

shtpl

bash templating

Install

Clone or download this repository:

git clone $p2git/dig/bash-utils.git

Put the shtpl script somewhere meant for executable scripts like /usr/local/bin/shtpl. Make it executable: chmod +x shtpl.

or let the installer do it for you :) bash-utils/shtpl/install

Usage

Create a template file to be executed with shtpl:

nano someTpl.shtpl
#!/usr/bin/env shtpl
Hello $WHO !

Make the template executable:

chmod +x someTpl.shtpl

... and execute it with some environment variables:

WHO=Doctor ./someTpl.shtpl
> Hello Doctor !

In case of many variable you can write 1 var on each line, but for bash to understand you have to escape the newlines and keep 1 space before:

VAR1=foo \
VAR2=bar \
VAR3=baz \
VAR4=aze \
./someTpl.shtpl

To save the result simply redirect to sdtout to a file:

WHO=Doctor ./someTpl.shtpl > doctorGreetings.txt

Templating syntax

shtpl uses a hack to turn you file into a call of echo "<your file here>", so it is simple, all what your system is able to understand as substitution is valid !

#!/usr/bin/env shtpl
Simple variable: $ENV_VAR , and with brackets: ${TOTO}cm²

Sub commands: $(echo ok)

Prompt user input in place: $(read -r -p Type\ some\ text\ : v;echo $v)

Loops: $(
for ((i=0 ; $MAX_LOOP - $i ; i++))
    do
	echo "[$i]"
done
)

Call sub templates: $(VAR_TO_PASS=$SOME_VAR ./myOtherTemplate.shtpl)
ENV_VAR=foo \
TOTO=42 \
MAX_LOOP=10 \
SOME_VAR=aze \
./someTpl.shtpl

Returns:

Simple variable: foo , and with brackets: 42cm²

Sub commands: ok

Prompt user input in place: your input

Loops: [0][1][2][3][4][5][6][7][8][9][10]

Call sub templates: what the template returns with "aze"

Additional helper functions

SHTPL declares somme utility functions to use in templates as a sub-commands, generally it is a 1 argument function that can be piped:

lowerCase

To transform a string in lowercase:

Original value: $value
Lowercase value: $(lowerCase $value)
Lowercase piped: $(echo $value | lowerCase)

slug

To transform a string in a safe name, special characters are turned into "_":

Original value: $value
Slug value: $(slug $value)
Slug piped: $(echo $value | slug)

user-input

Asks the user to enter text input at template execution and put it in place, multiline text is supported because you'd have to type (ctrl-D) to validate input:

You entered: $(user-input Enter a value)
and the 2nd time this: $(user-input 'Question with simple quote')
the last one is turned to lowercase: $(user-input To be turned in lowercase | lowerCase)