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.

Shell pipes and in-place insanity

15 pointsby fakedrakealmost 11 years ago

4 comments

jamesdutcalmost 11 years ago
I think this behaviour is supposed to be a feature of shell. Pipelines are executed in parallel.<p>Also, see `sponge` from `moreutils` (<a href="http://kitenet.net/~joey/code/moreutils/" rel="nofollow">http:&#x2F;&#x2F;kitenet.net&#x2F;~joey&#x2F;code&#x2F;moreutils&#x2F;</a>):<p><pre><code> $ man sponge ... sponge reads standard input and writes it out to the specified file. Un‐like a shell redirect, sponge soaks up all its input before opening the output file. This allows constructing pipelines that read from and write to the same file.</code></pre>
sikhnerdalmost 11 years ago
This is the expected behavior of pipes[0]. A quick look at Lists [1] clears up how you can control this a little better (+Job Control). jamesdutc also linked to sponge, which may suit your usecase as well.<p><pre><code> [0] https:&#x2F;&#x2F;www.gnu.org&#x2F;software&#x2F;bash&#x2F;manual&#x2F;bashref.html#Pipelines [1] https:&#x2F;&#x2F;www.gnu.org&#x2F;software&#x2F;bash&#x2F;manual&#x2F;bashref.html#Lists</code></pre>
mtdewcmualmost 11 years ago
That was a contrived example.<p>The shell sets up the pipe and file redirections for each process in the pipeline, then lets them go. It has no control over when the child processes read or write. It could be a race condition, but it&#x27;s written in such a way that it appears that the file will get truncated before it can be read virtually 100% of the time.
评论 #7960064 未加载
dredmorbiusalmost 11 years ago
NB: If you&#x27;re going to use CSS styling for code examples, choose foreground and background colors, as well as text sizes, which are actually legible.<p>Monospace fonts, black on white, at default sizes, are your best bets.
评论 #7960068 未加载
评论 #7960123 未加载