bash-utils/shtpl/README.md

127 lines
2.6 KiB
Markdown
Raw Normal View History

2018-10-09 17:08:34 +02:00
# shtpl
2019-03-02 21:25:53 +01:00
> bash templating
2018-10-09 17:08:34 +02:00
## Install
2019-03-02 21:25:53 +01:00
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`.
2018-10-09 17:08:34 +02:00
Make it executable: `chmod +x shtpl`.
2019-03-02 21:25:53 +01:00
or let the installer do it for you :)
`bash-utils/shtpl/install`
2018-10-09 17:08:34 +02:00
## Usage
Create a template file to be executed with shtpl:
2018-10-09 17:22:17 +02:00
```
nano someTpl.shtpl
```
```
2019-03-02 21:25:53 +01:00
#!/usr/bin/env shtpl
2018-10-09 17:22:17 +02:00
Hello $WHO !
```
2018-10-09 17:36:06 +02:00
Make the template executable:
2018-10-09 17:22:17 +02:00
```
chmod +x someTpl.shtpl
2018-10-09 17:36:06 +02:00
```
... and execute it with some environment variables:
```
WHO=Doctor ./someTpl.shtpl
2018-10-09 17:22:17 +02:00
```
```
> Hello Doctor !
```
2018-10-09 17:36:06 +02:00
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
```
2018-10-09 17:22:17 +02:00
## 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 !
```
2019-03-02 21:25:53 +01:00
#!/usr/bin/env shtpl
2018-10-09 17:22:17 +02:00
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
2018-10-09 17:36:06 +02:00
echo "[$i]"
2018-10-09 17:22:17 +02:00
done
)
Call sub templates: $(VAR_TO_PASS=$SOME_VAR ./myOtherTemplate.shtpl)
2018-10-09 17:36:06 +02:00
```
```
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"
2019-03-02 21:25:53 +01:00
```
2019-07-26 13:03:48 +02:00
## 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)
```