Struct subprocess::Pipeline [−][src]
pub struct Pipeline { /* fields omitted */ }A builder for multiple Popen instances connected via
pipes.
A pipeline is a sequence of two or more Exec commands
connected via pipes. Just like in a Unix shell pipeline, each
command receives standard input from the previous command, and
passes standard output to the next command. Optionally, the
standard input of the first command can be provided from the
outside, and the output of the last command can be captured.
In most cases you do not need to create Pipeline instances
directly; instead, combine Exec instances using the |
operator which produces Pipeline.
Examples
Execute a pipeline and return the exit status of the last command:
let exit_status = (Exec::shell("ls *.bak") | Exec::cmd("xargs").arg("rm")).join()?;
Capture the pipeline's output:
let dir_checksum = { Exec::cmd("find . -type f") | Exec::cmd("sort") | Exec::cmd("sha1sum") }.capture()?.stdout_str();
Methods
impl Pipeline[src]
impl Pipelinepub fn new(cmd1: Exec, cmd2: Exec) -> Pipeline[src]
pub fn new(cmd1: Exec, cmd2: Exec) -> PipelineCreates a new pipeline by combining two commands.
Equivalent to cmd1 | cmd2.
pub fn stdin<T: Into<InputRedirection>>(self, stdin: T) -> Pipeline[src]
pub fn stdin<T: Into<InputRedirection>>(self, stdin: T) -> PipelineSpecifies how to set up the standard input of the first command in the pipeline.
Argument can be:
- a
Redirection; - a
File, which is a shorthand forRedirection::File(file); - a
Vec<u8>or&str, which will set up aRedirection::Pipefor stdin, making sure thatcapturefeeds that data into the standard input of the subprocess. NullFile, which will redirect the standard input to read from /dev/null.
pub fn stdout<T: Into<OutputRedirection>>(self, stdout: T) -> Pipeline[src]
pub fn stdout<T: Into<OutputRedirection>>(self, stdout: T) -> PipelineSpecifies how to set up the standard output of the last command in the pipeline.
Argument can be:
- a
Redirection; - a
File, which is a shorthand forRedirection::File(file); NullFile, which will redirect the standard output to write to /dev/null.
pub fn stderr_to(self, to: File) -> Pipeline[src]
pub fn stderr_to(self, to: File) -> PipelineSpecifies a file to which to redirect the standard error of all the commands in the pipeline.
It is useful for capturing the standard error of the pipeline as a
whole. Unlike stdout(), which only affects the last command in
the pipeline, this affects all commands. The difference is
because standard output is piped from one command to the next, so
only the output of the last command is "free". In contrast, the
standard errors are not connected in any way. This is also the
reason only a File is supported - it allows for efficient
sharing of the same file by all commands.
pub fn popen(self) -> PopenResult<Vec<Popen>>[src]
pub fn popen(self) -> PopenResult<Vec<Popen>>Starts all commands in the pipeline, and returns a
Vec<Popen> whose members correspond to running commands.
If some command fails to start, the remaining commands
will not be started, and the appropriate error will be
returned. The commands that have already started will be
waited to finish (but will probably exit immediately due
to missing output), except for the ones for which
detached() was called. This is equivalent to what the
shell does.
pub fn join(self) -> PopenResult<ExitStatus>[src]
pub fn join(self) -> PopenResult<ExitStatus>Starts the pipeline, waits for it to finish, and returns the exit status of the last command.
pub fn stream_stdout(self) -> PopenResult<Box<Read>>[src]
pub fn stream_stdout(self) -> PopenResult<Box<Read>>Starts the pipeline and returns a Read trait object that
reads from the standard output of the last command.
This will automatically set up
stdout(Redirection::Pipe), so it is not necessary to do
that beforehand.
When the trait object is dropped, it will wait for the
pipeline to finish. If this is undesirable, use
detached().
pub fn stream_stdin(self) -> PopenResult<Box<Write>>[src]
pub fn stream_stdin(self) -> PopenResult<Box<Write>>Starts the pipeline and returns a Write trait object
that writes to the standard input of the first command.
This will automatically set up stdin(Redirection::Pipe),
so it is not necessary to do that beforehand.
When the trait object is dropped, it will wait for the
process to finish. If this is undesirable, use
detached().
pub fn capture(self) -> PopenResult<CaptureData>[src]
pub fn capture(self) -> PopenResult<CaptureData>Starts the pipeline, collects its output, and waits for all commands to finish.
The return value provides the standard output of the last command, the combined standard error of all commands, and the exit status of the last command. The captured outputs can be accessed as bytes or strings.
Unlike Popen::communicate, this method actually waits for the
processes to finish, rather than simply waiting for the output to
close. If this is undesirable, use detached().
Trait Implementations
impl Clone for Pipeline[src]
impl Clone for Pipelinefn clone(&self) -> Pipeline[src]
fn clone(&self) -> PipelineReturns a copy of the value.
This method is guaranteed not to fail as long as none of
the Redirection values contain a Redirection::File
variant. If a redirection to File is present, cloning
that field will use File::try_clone method, which
duplicates a file descriptor and can (but is not likely
to) fail. In that scenario, Exec::clone panics.
fn clone_from(&mut self, source: &Self)1.0.0[src]
fn clone_from(&mut self, source: &Self)Performs copy-assignment from source. Read more
impl BitOr<Exec> for Pipeline[src]
impl BitOr<Exec> for Pipelinetype Output = Pipeline
The resulting type after applying the | operator.
fn bitor(self, rhs: Exec) -> Pipeline[src]
fn bitor(self, rhs: Exec) -> PipelineAppend a command to the pipeline and return a new pipeline.
impl BitOr for Pipeline[src]
impl BitOr for Pipelinetype Output = Pipeline
The resulting type after applying the | operator.
fn bitor(self, rhs: Pipeline) -> Pipeline[src]
fn bitor(self, rhs: Pipeline) -> PipelineAppend a pipeline to the pipeline and return a new pipeline.
impl Debug for Pipeline[src]
impl Debug for Pipeline