简体   繁体   中英

call shell run pipeline failed in rust's Command.output

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);
}

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 to exit ).

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) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM