Re:Show HN: Stamp turns a folder into a plain text file and ...
by theamk on Sunday February 07, 2021 @02:55PM
(#26057677)
I prefer to use shell scripts for that, this works pretty well and is very readable:
it does not require any tools to extract either.
(and if you omit quotes around EOF, you can do substitutions)
[ Parent
| on HN ]
Re:Show HN: Stamp turns a folder into a plain text file and ...
by thaumaturgy on Sunday February 07, 2021 @03:32PM
(#26057949)
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/).
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.