bash-utils/shtpl/README.md

88 lines
1.6 KiB
Markdown

# shtpl
> sh templating
## Install
Put the `shtpl` script somewhere meant for executable scripts like `~/bin/shtpl`.
Make it executable: `chmod +x shtpl`.
## Usage
Create a template file to be executed with shtpl:
```
nano someTpl.shtpl
```
```
#!/path/to/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 !
```
#!/path/to/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"
```