TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Defer for Shell (2020)

91 pointsby harporoederalmost 2 years ago

8 comments

vermilinguaalmost 2 years ago
&gt; This is known as idempotency.<p>No? Idempotency is ensuring that calling the same function with the same input N times will have the exact same effect as calling it once. Adding this defer does _not_ guarantee idempotency, because it has no impact on the body of the script. You can provide just as much idempotency by running `rm -rf &#x2F;tmp&#x2F;$script_dir*` on the first line.
评论 #36417088 未加载
mplewis9zalmost 2 years ago
While this is great, this only cleans up temporary files on a clean exit (well, depending on the shell), and no trap can catch SIGKILL. I much prefer cleaning up my files _before_ I use them, like:<p>```<p>touch &#x27;temp.txt&#x27;<p>exec 3 &lt; &#x27;temp.txt&#x27;<p>exec 4 &gt; &#x27;temp.txt&#x27;<p>rm -f &#x27;temp.txt&#x27;<p>```<p>Now I have file descriptors 3 and 4 available for reading and writing, respectively, and even if I pulled the plug out from my desktop the file will not be anywhere on my filesystem.
评论 #36416631 未加载
评论 #36416016 未加载
评论 #36415501 未加载
评论 #36414991 未加载
abathuralmost 2 years ago
Heh! Not sure if it&#x27;s a cosmic coincidence, but I posted something (<a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=36410781">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=36410781</a>) similar based on <a href="https:&#x2F;&#x2F;github.com&#x2F;bashup&#x2F;events">https:&#x2F;&#x2F;github.com&#x2F;bashup&#x2F;events</a> a few hours ago in another thread.<p>The version here&#x27;s much simpler, which has its merit if you need the performance or have a simple (append-only, exit-only) use-case.<p>bashup.events supports removing handlers, one-time events, pseudo-promises, and more. It&#x27;s easy to use it for something like this, but also modular deferred init&#x2F;load and more.<p>(linked comment has a little more detail on how I use it.)
cevingalmost 2 years ago
I wrote something similar some years ago: <a href="https:&#x2F;&#x2F;codereview.stackexchange.com&#x2F;questions&#x2F;67417&#x2F;an-atexit-for-bash" rel="nofollow noreferrer">https:&#x2F;&#x2F;codereview.stackexchange.com&#x2F;questions&#x2F;67417&#x2F;an-atex...</a><p>I do not like the way you build expressions by concatenating strings. This is the driving force behind SQL injections.
评论 #36416025 未加载
_huayra_almost 2 years ago
I&#x27;m adding this to my dotfiles because it just seems so dang useful, but this really seems to be the intersection of elegance and hackery that&#x27;s the epitome of shell scripting. A close second would be that &quot;fork bomb&quot; that&#x27;s just a bunch of :{}|{} or something weird like that, but `defer` here is actually useful!
评论 #36416304 未加载
smitty1ealmost 2 years ago
This seems conceptually similar to python&#x27;s `with` statement.<p><a href="https:&#x2F;&#x2F;docs.python.org&#x2F;3&#x2F;reference&#x2F;compound_stmts.html#the-with-statement" rel="nofollow noreferrer">https:&#x2F;&#x2F;docs.python.org&#x2F;3&#x2F;reference&#x2F;compound_stmts.html#the-...</a>
评论 #36412924 未加载
评论 #36413547 未加载
JohnMakinalmost 2 years ago
I’m trying to figure out a way this can backfire horribly but falling short of it. Curious to test the interaction with set -e if your defer command also errors. Very clever.
评论 #36413228 未加载
评论 #36413303 未加载
评论 #36413582 未加载
archargelodalmost 2 years ago
I know, so far, only two languages that use defer: Go and Nim.
评论 #36413309 未加载
评论 #36413997 未加载
评论 #36413044 未加载
评论 #36413013 未加载
评论 #36413115 未加载
评论 #36457819 未加载