use std::process::{Command, Stdio};
use std::str;
fn main() {
let cat = Command::new("cat")
.arg("abc") // Make the command fail
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()
.unwrap();
// Maybe more piped commands here
// ...
let echo = Command::new("echo")
.stdin(Stdio::from(cat.stdout.unwrap()))
.stdout(Stdio::piped())
.spawn()
.unwrap();
let output = echo.wait_with_output().unwrap();
println!("code: {:?}", output.status.code());
println!("output: {}", str::from_utf8(&output.stdout).unwrap());
if let Some(code) = output.status.code() {
if code != 0 {
std::process::exit(code);
}
}
}
In the example above, the ultimate goal of the program is to get the final output of multiple piped subprocesses. However, If the program should terminate if any of the processes fail, how should we check the status of each process?
Checking the final command seems the wrong way to it as in this example it always return 0
code even the first command failed.
What is the best practice of this?
Thank!