Skip to content

Instantly share code, notes, and snippets.

@x-yuri
Created July 4, 2024 02:08
Show Gist options
  • Save x-yuri/be7503faf762433eed3acb1abb722bd3 to your computer and use it in GitHub Desktop.
Save x-yuri/be7503faf762433eed3acb1abb722bd3 to your computer and use it in GitHub Desktop.
sh template engines

sh template engines

a.tpl:

a: <% $a %>
% if [ "$b" ]; then
b: <% $b %>
% fi
c: <% $c %>

a.sh:

docker run --rm -v ./a.tpl:/a.tpl alpine:3.20 sh -euxc '
    apk add curl bash
    curl https://raw.githubusercontent.com/TekWizely/bash-tpl/v0.7.1/bash-tpl \
        -o bash-tpl
    bash bash-tpl a.tpl > a.sh
    cat a.sh
    a=1 b=2 c=3 sh a.sh
    a=1 c=3 sh a.sh
'
$ sh a.sh
...
+ cat a.sh
printf "%s\n" a:\ "$a"
if [ "$b" ]; then
printf "%s\n" b:\ "$b"
fi
printf "%s\n" c:\ "$c"
+ a=1 b=2 c=3 sh a.sh
a: 1
b: 2
c: 3
+ a=1 c=3 sh a.sh
a: 1
c: 3

b.tpl:

a: $a
{% if $b %}
b: $b
{% endif %}
c: $c

b.sh:

docker run --rm -v ./b.tpl:/b.tpl alpine:3.20 sh -euxc '
    apk add curl bash coreutils
    curl https://raw.githubusercontent.com/ml0renz0/shtpl/31798223cfbc12a98c9c6d6de5c1e20add8e8aea/shtpl \
        -o shtpl
    cat b.tpl | a=1 b=2 c=3 bash -e shtpl
    cat b.tpl | a=1 c=3 bash -e shtpl
'
$ sh b.sh
...
+ cat b.tpl
+ a=1 b=2 c=3 bash -e shtpl
a: 1
b: 2
c: 3
+ cat b.tpl
+ a=1 c=3 bash -e shtpl
a: 1
c: 3

c.mo:

a: {{a}}
{{#b}}
b: {{b}}
{{/b}}
c: {{c}}

c.sh:

docker run --rm -v ./c.mo:/c.mo alpine:3.20 sh -euxc '
    apk add curl bash
    curl https://raw.githubusercontent.com/tests-always-included/mo/3.0.5/mo \
        -o mo
    chmod u+x mo
    a=1 b=2 c=3 ./mo c.mo
    a=1 c=3 ./mo c.mo
'
$ sh c.sh
...
+ a=1 b=2 c=3 ./mo c.mo
a: 1
b: 2
c: 3
+ a=1 c=3 ./mo c.mo
a: 1
c: 3

d.tpl:

a: $a
#% if [ "$b" ]; then
b: $b
#% fi
c: $c

d.sh:

docker run --rm -v ./d.tpl:/d.tpl alpine:3.20 sh -euxc '
    apk add curl
    curl https://raw.githubusercontent.com/dontsueme/shtpl/0.6/shtpl \
        -o shtpl
    set +e; sh shtpl d.tpl > d.sh; set -e
    a=1 b=2 c=3 sh d.sh
    a=1 c=3 sh d.sh
'
$ sh d.sh
...
+ a=1 b=2 c=3 sh d.sh
a: 1
b: 2
c: 3
+ a=1 c=3 sh d.sh
a: 1
c: 3

e.esh:

a: <%= $a %>
<% if [ "${b-}" ]; then %>b: <%= $b %>
<% fi %>c: <%= $c %>

e.sh:

docker run --rm -v ./e.esh:/e.esh alpine:3.20 sh -euxc '
    apk add esh
    a=1 b=2 c=3 esh e.esh
    a=1 c=3 esh e.esh
'
$ sh e.sh
...
+ a=1 b=2 c=3 esh e.esh
a: 1
b: 2
c: 3
+ a=1 c=3 esh e.esh
a: 1
c: 3

f.sc:

a: <$ echo -n $a $>
<$ if [ "$b" ]; then $>b: <$ echo -n $b $>
<$ fi $>c: <$ echo -n $c $>

f.sh:

docker run --rm -v ./f.sc:/f.sc alpine:3.20 sh -euxc '
    apk add shellcat
    a=1 b=2 c=3 shellcat f.sc
    a=1 c=3 shellcat f.sc
'
$ sh f.sh
...
+ a=1 b=2 c=3 shellcat f.sc
a: 1
b: 2
c: 3
+ a=1 c=3 shellcat f.sc
a: 1
c: 3

g.tpl:

a: {{a}}
{{if {{b}}:
"b: " {{b}}
}}
c: {{c}}

g.sh:

docker run --rm -v ./g.tpl:/g.tpl alpine:3.20 sh -euxc '
    apk add curl bash
    curl https://raw.githubusercontent.com/husixu1/bpt/v0.3/bpt.sh \
        -o bpt.sh
    bash bpt.sh g g.tpl > g.sh
    a=1 b=2 c=3 sh g.sh
    a=1 c=3 sh g.sh
'
$ sh g.sh
...
+ a=1 b=2 c=3 sh g.sh
a: 1
b: 2
c: 3
+ a=1 c=3 sh g.sh
a: 1

c: 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment