TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Shell pipes and in-place insanity

15 点作者 fakedrake将近 11 年前

4 条评论

jamesdutc将近 11 年前
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>
sikhnerd将近 11 年前
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>
mtdewcmu将近 11 年前
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 未加载
dredmorbius将近 11 年前
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 未加载