Diff: NamedPipesInBash

Differences between version 3 and previous revision of NamedPipesInBash.

Other diffs: Previous Major Revision, Previous Author

Newer page: version 3 Last edited on October 11, 2007 10:34 am by PhilHollenback Revert
Older page: version 2 Last edited on January 14, 2007 6:37 pm by PhilHollenback Revert
@@ -33,4 +33,30 @@
 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  
+<!-- AddThis Bookmark Button BEGIN -->  
+<script type="text/javascript">  
+ addthis_url = location.href;  
+ addthis_title = document.title;  
+ addthis_pub = 'tels7ar';  
+</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>  
+<!-- AddThis Bookmark Button END -->  
+?>  
+-----  
+<?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 3

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
RecentChanges Click to hide/show