Bash_Scripts/Misc/Templater.md

49 lines
1.3 KiB
Markdown
Raw Normal View History

2021-11-13 21:23:28 -06:00
# Bash Templater
## Variable Types
All variables are used in the following format:
```
{#% VARIABLE_TYPE=data %#}
```
For example
```
{#% FILE=~/myfile.content %#}
```
- FILE
- File will read the content from the given file if it exists
- WRITE-TO
- This is used to tell the current process where to write to, only one should exist
in a template and sub-templates if multiple templates are chained together
- TEMPLATE
- This is used to parse the contents of another template and include the contents that are parsed.
2021-11-13 21:23:28 -06:00
For example, say we have a primary template `main.tmpl` that uses the sub-template `sub.tmpl` then
when the parsing process begins it will also parse the contents of `sub.tmpl` and append them to the contents
parsed by `main.tmpl`
- VAR
- This is used when you want to expand a variable from your environment, say "${HOME}"
- EVAL
- This is used when you want to evaluate a statement in bash, be careful as any errors here can really nuke things and
cause security issues.
## Example
Given a template file with content like so:
```
{#% VAR=$HOME %#}
{#% EVAL="ls -alh" %#}
```
The output would be something like
```
/home/username
total 52K
drwxr-xr-x 1 sam sam 196 Sep 15 17:03 .
drwxr-xr-x 1 sam sam 94 Sep 15 16:42 ..
-rwxr-xr-x 1 sam sam 13K Sep 15 17:01 Templater.bash
-rw-r--r-- 1 sam sam 1.1K Sep 15 17:03 Templater.md
```