Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to get stderr #43

Open
RickSKy opened this issue Aug 18, 2022 · 1 comment
Open

how to get stderr #43

RickSKy opened this issue Aug 18, 2022 · 1 comment
Labels
question Further information is requested

Comments

@RickSKy
Copy link

RickSKy commented Aug 18, 2022

Is there any example to get stderr for expect?

@zhiburt
Copy link
Owner

zhiburt commented Aug 18, 2022

Hi @RickSKy
Thank you for the question.

As far as I understand the question you what to read from stderr right?

expect reads from stdout and stderr simultaneously so there's only 1 way I know how to do it.
You need to turn of stdout.
See this example.

use std::process::Stdio;

use expectrl::{Error, Session, WaitStatus};

fn main() -> Result<(), Error> {
    let mut cmd = std::process::Command::new("sh");
    cmd.arg("-c").arg("echo print to stdout && echo print to stderr >&2");

    // turn off stdout
    cmd.stdout(Stdio::null());

    let mut p = Session::spawn(cmd)?;

    let line = p.expect('\n')?;
    println!("{:?}", String::from_utf8_lossy(line.as_bytes()));

    let line = p.expect('\n')?;
    println!("{:?}", String::from_utf8_lossy(line.as_bytes()));

    assert_eq!(p.wait().unwrap(), WaitStatus::Exited(p.pid(), 0));

    Ok(())
}

Am I addressed you question?

@zhiburt zhiburt added the question Further information is requested label Aug 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants