|
| From: | Rob Landley |
| Subject: | Re: potential addition of sponge to POSIX |
| Date: | Thu, 26 Jun 2025 15:06:56 -0500 |
| User-agent: | Mozilla Thunderbird |
On 6/21/25 08:56, Marc Chantreux wrote:
On Fri, Jun 20, 2025 at 09:21:28AM -0400, Michael Stone wrote:I agree with this, and question why the austin group is considering it at all.well. I shell almost every day for about 30 years now and I'm really happy about this because sponge is so convenient.
Can't you just do:
sponge() { local X=$(base64); base64<<<"$X" > $1; }
(Mostly you can do it with X=$(cat) instead and avoid doubling the
memory but that can't handle embedded NUL bytes, and I know of no way to
convince bash not to strip trailing newlines from $() invocations, so
base64 is the data safe way.)
I asked for opinions about this on the toybox list and the Android base OS maintainer wasn't impressed either:
http://lists.landley.net/pipermail/toybox-landley.net/2025-June/030741.htmlBeyond the "this really doesn't come up much" problem, sed -i and patch and friends can know if there was an error in processing and keep the old file. Sponge in a pipe doesn't know if the command piping into it failed, so unless you define "no input" as keeping the old file intact (which still doesn't catch a lot of error cases) it will replace the output file whether you want it to or not, it just delays doing it slightly so you can use the same input and output.
To be honest, every time I've needed something like this... isn't that what mktemp is for?
thingy <"$IN" > ${BLAH:=$(mktemp .blahxxxxx)} && mv -f $BLAH "$IN" ||
{ rm -rf $BLAH; return 1; }
Error checking and everything. You can even have a trap handler do the
rm if you're feeling posh.
Rob
| [Prev in Thread] | Current Thread | [Next in Thread] |