View Source:
NamedPipesInBash
Note:
This page has been locked and cannot be edited.
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.: <verbatim> # /sbin/lsmod >/tmp/lsmod.tmp # ssh sys1 /sbin/lsmod | diff /tmp/lsmod.tmp - </verbatim> 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: <verbatim> # diff <(/sbin/lsmod) <(ssh sys1 /sbin/lsmod) </verbatim> 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. CategoryGeekStuff CategoryBlog
Please enable JavaScript to view the
comments powered by Disqus.
HollenbackDotNet
Home Page
Popular Pages
All Categories
Main Categories
General Interest
Geek Stuff
DevOps
Linux Stuff
Pictures
Search
Toolbox
RecentChanges
RecentNewPages
What links here
Printable version
AllPages
RecentChanges
Recent Changes Cached
No changes found
Favorite Categories
ActionPage
(150)
WikiPlugin
(149)
GeekStuff
(137)
PhpWikiAdministration
(102)
Help/PageList
(75)
Help/MagicPhpWikiURLs
(75)
Blog
(69)
Pictures
(60)
GeneralInterest
(44)
LinuxStuff
(38)
Views
View Page
View Source
History
Diff
Sign In