3 ms·
M4 is so impotent and crippling difficult to use, that it led me to create my own dumb templating engine[0] because there is no standard POSIX tool that will le
by luizfelberti 7y ago
M4 is so impotent and crippling difficult to use, that it led me to create my own dumb templating engine[0] because there is no standard POSIX tool that will let you, simply and in the dumbest way possible, substitute strings within a file.
If you try using awk or sed, you will need to figure out a magical way to escape whatever arbitrary value you are trying to replace, so you don't break sed's regex or awk's syntax.
M4 was one of the last things I tried, and it was an utter disaster. If any of M4's magical characters collides with the syntax of the file you're trying to template: tough luck. And there is no way to escape your file for M4 either, it's syntax doesn't support that.
I eventually discovered that Bash 4.4+ has some built-in functions for "dumb" string substitution[1], which is exactly what's missing from the default POSIX tools.
Since then I wrote this little wrapper around that functionality, as a simple, single-file, dependency-free Bash script. If you find something like that useful, it's on the link below:
[0] https://github.com/luizberti/templ https://github.com/luizberti/templ
[1] https://www.tldp.org/LDP/abs/html/string-manipulation.html https://www.tldp.org/LDP/abs/html/string-manipulation.html
- diroussel 7y agoWould perl or ruby not suit your requirements?
- luizfelberti 7y agoRuby most definitely wouldn't, it doesn't come pre-installed on almost any AMI/server/Docker Image/etc. As for perl, it does come "with the system" more often than Ruby, but also not as much as bash. Even if it did, I reckon bash is something that nowadays people are more accustomed to than perl, and would consider it more of a no brainer.
- MarkSweep 7y agoThere is likely already a simple templating program installed on your linux OS: envsubst. It is included as part of the gettext-base package on Ubuntu and installed by default I believe. https://www.gnu.org/software/gettext/manual/html_node/envsubst-Invocation.html https://www.gnu.org/software/gettext/manual/html_node/envsub... The templating language looks similar to capability to that provided by templ.
- vePhi4le 7y ago`envsubst` is not POSIX, but my preferred tool for such things: https://linux.die.net/man/1/envsubst https://linux.die.net/man/1/envsubst it's part of `gettext`: https://www.gnu.org/software/gettext/manual/html_node/envsubst-Invocation.html https://www.gnu.org/software/gettext/manual/html_node/envsub...