Diff: NamedPipesInBash

Differences between version 6 and previous revision of NamedPipesInBash.

Other diffs: Previous Major Revision, Previous Author

Newer page: version 6 Last edited on February 29, 2012 11:24 am by PhilHollenback Revert
Older page: version 5 Last edited on January 28, 2009 2:13 pm by PhilHollenback Revert
@@ -33,33 +33,4 @@
 Note you can stuff stdout __into__ named pipes with the >(foo) 
 operator too, thus the output of a process can be sent into another 
 process which you invoke on the command line. This is more flexible 
 than the standard pipe mechanism. 
-  
------  
-  
-<?plugin RawHtml  
-<script>  
-var idcomments_acct = '011e5665a1128cdbe79c8077f0f04353';  
-var idcomments_post_id;  
-var idcomments_post_url;  
-</script>  
-<span id="IDCommentsPostTitle" style="display:none"></span>  
-<script type='text/javascript' src='http://www.intensedebate.com/js/genericCommentWrapperV2.js'></script>  
-?>  
-  
------  
-  
-<?plugin RawHtml  
-<center>  
-<script type="text/javascript"><!--  
-google_ad_client = "pub-5011581245921339";  
-google_ad_width = 728;  
-google_ad_height = 90;  
-google_ad_format = "728x90_as";  
-google_ad_channel ="";  
-//--></script>  
-<script type="text/javascript"  
- src="http://pagead2.googlesyndication.com/pagead/show_ads.js">  
-</script>  
-</center>  
-?>  

version 6

Here's something that somebody just recently told me. This is a useful and completely non-obvious tip, so I'm documenting it here in hopes that others will find it useful.

My specific problem was that I wanted to diff the output of two processes. You can't do this with normal shell reidrection, the best you can do is:

  • send the output of one process to a tempfile
  • diff the tempfile and the output of the second process, i.e.:
# /sbin/lsmod >/tmp/lsmod.tmp
# ssh sys1 /sbin/lsmod | diff /tmp/lsmod.tmp -

that works, but it requires a temporary file.

The answer to this problem is process substitution. Bash has some process substitution operators (<(foo) and >(foo)) which are very poorly explained in the bash man page (and trust me, I've read that man page a lot). foo can be any command that produces output on stdout. Bash execs the command, creates a named pipe from the output, and replaces the operator with the name of that pipe. You can then read stdout from that pipe as you would from a regular file. Thus in this case the output of foo might be fed through the file /dev/fd/64. Now our diff example can be written like this:

# diff <(/sbin/lsmod) <(ssh sys1 /sbin/lsmod)

Note you can stuff stdout into named pipes with the >(foo) operator too, thus the output of a process can be sent into another process which you invoke on the command line. This is more flexible than the standard pipe mechanism.



Our Founder
ToolboxClick to hide/show