Reputation: 15992
I'm trying to get result from call shell and grep the result. But it failed, in shell, it works.
use std::process::Command;
fn main() {
let result = Command::new("sh")
.arg("-c")
.arg("last") // by this line it works
// .arg("last | grep 'still logged in'") // by this line, it will return 256 code
.output()
.expect("'last' command failed to start");
println!("{:?}", result);
}
Upvotes: 2
Views: 138
Reputation: 70840
https://doc.rust-lang.org/stable/std/process/struct.ExitStatus.html
An
ExitStatus
represents every possible disposition of a process. On Unix this is the wait status. It is not simply an exit status (a value passed toexit
).
See I used wait(&status) and the value of status is 256, why?. grep
returns 1
as no match was found, and this becomes an exist status of 256
.
If you want the exit code, call output.status.code()
. This correctly returns Some(1)
.
Upvotes: 2