Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First, let’s look at how the shell’s <code>&lt;(command)</code> command substitution works. The command is started with the output pointing to a newly created pipe. Then the pipe is passed on the command line as a <em>filename</em> to the outer command.</p> <p>For example:</p> <pre><code>$ echo wc -w &lt;(date) wc -l /dev/fd/63 </code></pre> <p>Shows that the shell starts <code>date</code> with output to a pipe, and then passes the pseudofile <code>/dev/fd/63</code> referring to the new pipe at file descriptor 63 as the actual command line argument.</p> <hr> <p>To get the same behaviour in Python, we can implement the same thing:</p> <pre><code>from subprocess import * date = Popen(['date'], stdout=PIPE) Popen(["wc", "-w", "/dev/fd/%d" % date.stdout.fileno()]) </code></pre> <p>print</p> <pre><code>6 /dev/fd/3 </code></pre> <hr> <p>You could write a class to make it easier to do. This uses a <a href="http://www.python.org/dev/peps/pep-0343/" rel="nofollow"><code>with</code> statement</a> and temporarily keeps references to each spawned process, because otherwise the pipes would get closed when the process objects got garbage-collected.</p> <pre><code>from subprocess import * class CommandSubstituter(object): def __init__(self): self._procs = [] def __enter__(self): return self def __exit__(self, *exc_info): pass def subst(self, *args, **kwargs): proc = Popen(*args, stdout=PIPE, **kwargs) self._procs.append(proc) return "/dev/fd/%d" % proc.stdout.fileno() with CommandSubstituter() as c: Popen(['wc', '-l', c.subst(['comm', '-12', c.subst(['sort', 'file1.txt']), c.subst(['sort', 'file2.txt'])])]) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload