The beauty of shell scripts
Published
, last updated
I recently decided i needed a personal website to showcase projects and publish articles.
So i looked into ways to do that and quickly found Zola.
The setup was easy and before long i had a working prototype.
Then i looked into ways to deploy my site. I’ve already setup Caddy
on my server for some other projects, so the only things i needed to do was to put the output of Zola
on the server and create a new site block
with a file_server in my Caddyfile.
chjlsch.ch {
root * /srv/www/chjlsch.ch/public
file_server
}
After creating some more elaborate templates
and adding some dummy articles, i looked into optimisations and settled on the following two:
After some searching i found these two amazing tools:
- minify “minifiers for web formats”
- zstd “fast lossless compression algorithm”
I use Alpine Linux on my server, so installing them is quick:
apk add minify zstd
Zola outputs all of the files in a directory called public, so to minify all files i just call:
minify -ri public
This minifies all files in-place (-i) recursively (-r) in the directory public.
Then i compress the files using zstd, which is simmilarly simple:
zstd -19r public
This compresses all files in public recursively (-r) using compression level 19 (-19).
This also compresses the woff2 font files, which is unnecessary,
because they already are compressed.
To circumvent that, i create a temporary file called .tmp.zstd in which i list all files that need to be compressed.
I get that list using find and then filter out the .woff2 files using ripgrep.
Then i just pass that file along to zstd:
find public -type f | rg -v "\.woff.?$" > .tmp.zstd
zstd -19 --filelist .tmp.zstd
So the complete script is just five lines long:
# build.sh
zola build
minify -ri public
find public -type f | rg -v "\.woff.?$" > .tmp.zstd
zstd -19 --filelist .tmp.zstd
rm -f .tmp.zstd
I like this solution, it’s simple and reliable.
Instead of having a big and complex build system with lots of configuration,
i have a simple shell script which does everything i need.
It’s also quite fast, building my site in under 100ms (~70ms spent on compression with zstd).
¶
Bash pipes
By the way, i learned something about pipes in bash. If you redirect output of a command like find into a file,
the file gets created before the command gets executed.
So if you have directory with the files alice.txt and bob.txt and then you run find > list.txt,
you get the following output:
alice.txt
bob.txt
list.txt