簡體   English   中英

打印命令 output

[英]Printing output of command

我正在使用gtk-rs獲取輸入值並使用它來執行帶有xdotool的命令:

 input.connect_activate(clone!(@weak window => move |entry| {
    let input_text = entry.text();
    let cmd = format!(
        "xdotool search --onlyvisible --name {} windowactivate",
        input_text
    );

    let output = std::process::Command::new("sh")
        .arg("-c")
        .arg(cmd)
        .spawn()
        .unwrap();

    println!("{}", String::from_utf8_lossy(&output.stdout));

    window.close();
}));

我收到此編譯錯誤:

error[E0308]: mismatched types
   --> src/main.rs:66:48
    |
66  |         println!("{}", String::from_utf8_lossy(&output.stdout));
    |                        ----------------------- ^^^^^^^^^^^^^^ expected slice `[u8]`, found enum `std::option::Option`
    |                        |
    |                        arguments to this function are incorrect
    |
    = note: expected reference `&[u8]`
               found reference `&std::option::Option<ChildStdout>`
note: associated function defined here
   --> /home/alex/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/string.rs:631:12
    |
631 |     pub fn from_utf8_lossy(v: &[u8]) -> Cow<'_, str> {
    |            ^^^^^^^^^^^^^^^

所以我嘗試了這個:

match output.stdout {
    Some(stdout) => {
        println!("{}", String::from_utf8_lossy(&stdout));
    },
    None => {
        println!("No output");
    }
}

但我遇到了同樣的錯誤。

更新:

我試過:

let output = std::process::Command::new("sh")
    .arg("-c")
    .arg(cmd)
    .output()
    .unwrap();

println!(Output: "{}", String::from_utf8_lossy(&output.stdout));

但是沒有打印任何內容:

Output: 

完整代碼: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=ea4431b73191c132f4392378cce13604

閱讀評論和文檔后,我設法打印命令:

println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
println!("stderr: {}", String::from_utf8_lossy(&output.stderr));

但我必須這樣做: xdotool search --onlyvisible --name {}因為xdotool search --onlyvisible --name {} windowactivate什么都不打印。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM