3 ms·
I use this pattern a lot along with a tool I built for doing server deployments and administration using plain old shell scripts and ssh (golem: https://github.
by thaumaturgy 6y ago
I use this pattern a lot along with a tool I built for doing server deployments and administration using plain old shell scripts and ssh (golem: https://github.com/robsheldon/golem/ https://github.com/robsheldon/golem/).
There are two caveats:
First, if there's any chance at all that the heredoc may contain a $, or a `, or possibly some other shell-magical characters, then you have to use a single-quoted heredoc:
cat <<'EOF'...
This means that if you want to do variable interpolation, like you're doing, then you need something that looks like:
cat <<'EOF' | sed -e "s/\\\$username/$username/g" -e "s/\\\$my_email/$my_email/g" ...
It looks yucky and unwieldy at first, but I've found that it's nice to be able to see at the top of the heredoc exactly what's getting replaced and what values it needs.
Second, if root privileges are required to write the file, then you need to use `tee`, because you can't sudo an output redirection:
cat <<'EOF' | sed -e "s/\\\$username/$username/g" | sudo tee /path/to/file >/dev/null
After using it for a while, I've found I really like this pattern for managing configuration file templates.
You're right though that something like Stamp could be built using standard shell tools if someone were so inclined.
- waheoo 6y agohttps://www.gnu.org/software/gettext/manual/html_node/envsubst-Invocation.html https://www.gnu.org/software/gettext/manual/html_node/envsub...