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.

Pipes and Filters

223 pointsby rbcover 10 years ago

17 comments

js2over 10 years ago
Some random thoughts:<p>• Though the first pipeline is didactic, it can be done entirely within awk:<p><pre><code> awk &#x27; BEGIN { l=0 } &#x2F;purple&#x2F; { if(length($1) &gt;= l) { word = $1; l = length($1) } } END { print word }&#x27; &lt; &#x2F;usr&#x2F;share&#x2F;dict&#x2F;words </code></pre> • Named pipes are neat, but you can also use subshells and additional FDs (I am in no way arguing this is more clear):<p><pre><code> ( ( ( echo out echo err &gt;&amp;2 ) | while read out; do echo &quot;out: $out&quot;; done &gt;&amp;3 ) 2&gt;&amp;1 | while read err; do echo &quot;err: $err&quot;; done ) 3&gt;&amp;1 </code></pre> • Bash has &quot;set -o pipefail&quot; for cases where you want any process in the pipeline that exits non-zero to cause the entire pipeline to exit non-zero.
robertduncanover 10 years ago
Error detection is much easier with pipefail:<p><a href="http://www.gnu.org/software/bash/manual/html_node/Pipelines.html" rel="nofollow">http:&#x2F;&#x2F;www.gnu.org&#x2F;software&#x2F;bash&#x2F;manual&#x2F;html_node&#x2F;Pipelines....</a><p>&quot;If pipefail is enabled, the pipeline’s return status is the value of the last (rightmost) command to exit with a non-zero status, or zero if all commands exit successfully&quot;
评论 #8328040 未加载
bkirwiover 10 years ago
If you like pipes, you&#x27;ll love Pipe Viewer:<p><a href="http://www.ivarch.com/programs/pv.shtml" rel="nofollow">http:&#x2F;&#x2F;www.ivarch.com&#x2F;programs&#x2F;pv.shtml</a><p>&gt; pv - Pipe Viewer - is a terminal-based tool for monitoring the progress of data through a pipeline. It can be inserted into any normal pipeline between two processes to give a visual indication of how quickly data is passing through, how long it has taken, how near to completion it is, and an estimate of how long it will be until completion.
jstschover 10 years ago
Another fun trick, by piping through dd you can add a buffer between processes.<p>Example: the raspberry pi has pretty slow SD performance and the USB bus can get hogged. If you record audio and want to encode + write it to SD you can easily get buffer overruns. Easily solved by a 10sec buffer between arecord and flac in my case.
评论 #8329267 未加载
评论 #8328445 未加载
评论 #8327765 未加载
cghover 10 years ago
Reminds me of the classic David Beazley course on coroutines: <a href="http://www.dabeaz.com/coroutines/" rel="nofollow">http:&#x2F;&#x2F;www.dabeaz.com&#x2F;coroutines&#x2F;</a><p>It highlights a similar pipeline-oriented architecture and eventually ends up being sort of mindblowing.
评论 #8327775 未加载
brianpgordonover 10 years ago
I hate to be &quot;that guy&quot; but <i>someone</i> has to say something about the Useless Use of Cat.<p><a href="http://www.catb.org/jargon/html/U/UUOC.html" rel="nofollow">http:&#x2F;&#x2F;www.catb.org&#x2F;jargon&#x2F;html&#x2F;U&#x2F;UUOC.html</a>
评论 #8327967 未加载
评论 #8327633 未加载
daveloyallover 10 years ago
Off topic: Today I learned <a href="http://dcurt.is/unkudo" rel="nofollow">http:&#x2F;&#x2F;dcurt.is&#x2F;unkudo</a>. Peter Sobot, I want my kudos back. (Not that I didn&#x27;t really appreciate learning about ${PIPESTATUS[*]}.)
评论 #8327298 未加载
评论 #8328619 未加载
评论 #8329162 未加载
评论 #8328047 未加载
Anthony-Gover 10 years ago
I’ve used pipes for years and had a pretty good understanding of how the system calls of each process in the pipeline interacts with their own `sdin` and `stdout` file descriptors but this article puts it all together really nicely with some good examples.<p>I don’t mind the useless use of `cat` as it can enhance readability for some people. However, I would suggest replacing the Bash while loop with a for loop:<p><pre><code> ls *.flac | while read song do flac -d &quot;$song&quot; --stdout | lame -V2 - &quot;$song&quot;.mp3 done for song in *.flac do flac -d &quot;$song&quot; --stdout | lame -V2 - &quot;$song&quot;.mp3 done </code></pre> Using Bash’s file globbing avoids problems with enumerating the output of `ls` [1]. It also avoids an unnecessary Bash sub-shell being spawned for the while loop that follows the first pipe. More importantly, I think it’s a lot more readable while still demonstrating how pipes can be efficiently used to process any amount of FLAC files.<p>[1] <a href="http://mywiki.wooledge.org/ParsingLs" rel="nofollow">http:&#x2F;&#x2F;mywiki.wooledge.org&#x2F;ParsingLs</a>
wheeover 10 years ago
Neat. I have my own take on this concept[1] using Redis pub&#x2F;sub instead of queues. Tradeoffs involve being able to lose data if endpoints aren&#x27;t connected, but you do get the benefit of having multiple inputs and outputs on one stream, which was important for my use case.<p>[1] <a href="https://github.com/whee/rp" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;whee&#x2F;rp</a>
daemonizeover 10 years ago
I recommend highland.js for node <a href="http://highlandjs.org" rel="nofollow">http:&#x2F;&#x2F;highlandjs.org</a>
runeksover 10 years ago
I recently came across a language called Factor that works very similar, if not identical, to this.<p>Here&#x27;s a video about it: <a href="https://www.youtube.com/watch?v=f_0QlhYlS8g" rel="nofollow">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=f_0QlhYlS8g</a>
评论 #8327454 未加载
评论 #8327464 未加载
deathanatosover 10 years ago
<p><pre><code> _____________ &lt; unimpurpled &gt; ------------- \ \ </code></pre> Part way through my second viewing of the article, I thought, &quot;what is &#x27;unimpurpled&#x27;&quot;. Wiktionary didn&#x27;t know. Google doesn&#x27;t return useful results for it, even. M-W, finally, clued me in: it&#x27;s an obsolete term, with an &quot;un&quot; prefix, for the verb &quot;empurple&quot;, which means to make purple[1].<p>[1] And a few similar things. <a href="https://en.wiktionary.org/wiki/empurple" rel="nofollow">https:&#x2F;&#x2F;en.wiktionary.org&#x2F;wiki&#x2F;empurple</a>
tomggover 10 years ago
I&#x27;ve been playing around with julia[1] this week and discovered the inclusion of a pipe-like operator that removes a lot of the parentheses from functional programming; you can write,<p><pre><code> x |&gt; a|&gt; b |&gt; c|&gt;s-&gt;d(s,y)|&gt;e|&gt;... </code></pre> in julia instead of<p><pre><code> e(d(c(b(a(x))),y)) or (e (d (c (b (a x)) y)) </code></pre> ...or whatever is your flavour. I reckon it is impossible to make a serious case against that readability gain.<p>[1] julialang.org
评论 #8329756 未加载
评论 #8329678 未加载
评论 #8330566 未加载
评论 #8329844 未加载
评论 #8329714 未加载
pdknskover 10 years ago
&gt; I’m calling &#x2F;usr&#x2F;bin&#x2F;time here to avoid using my shell’s built-in time command [...].<p>I prefer to use command in this scenario.<p>$ command time
visargaover 10 years ago
Pipes are also related to the IO monad, similar in a way with the jQuery syntax which is another hugely popular case. I am utterly amazed how they invented such a powerful concept of functional programming for the shell (well, not 100% pure functional, if there are side effects)
评论 #8328708 未加载
xiaqover 10 years ago
The first few parts make a pretty good pipeline preach. Two interesting points about pipelines are the concurrency and the difficulty to handle errors (at least without cluttering the syntax), which are often missed by newcomers.
yellowappleover 10 years ago
The author has earned him&#x2F;herself a Useless Use of cat (UUOC) award for not realizing that grep can take a filename argument in the example pipeline.<p>Basically, the example can be shortened to the following:<p><pre><code> grep purple &#x2F;usr&#x2F;share&#x2F;dict words | # Find words containing &#x27;purple&#x27; in the system&#x27;s dictionary awk &#x27;{print length($1), $1}&#x27; | # Count the letters in each word sort -n | # Sort lines (&quot;${length} ${word}&quot;) tail -n 1 | # Take the last line of the input cut -d &quot; &quot; -f 2 | # Take the second part of each line cowsay -f tux # Put the resulting word into Tux&#x27;s mouth</code></pre>