代码之家  ›  专栏  ›  技术社区  ›  user103766

为什么compgen命令可以在Linux终端上工作,但不能与process::command一起工作?

  •  1
  • user103766  · 技术社区  · 7 年前

    命令 compgen 在Linux终端中工作正常,但在使用命令::new时会引起恐慌。

    我的代码:

    use std::process::Command;
    
    fn main() {
        let status = Command::new("compgen")
            .status()
            .expect("failed to execute process");
        println!("process exited with: {}", status);
    }
    

    错误消息:

        Compiling terminal v0.1.0 (file:///home/user/programmates/RUST/Terminal/terminal)
         Finished dev [unoptimized + debuginfo] target(s) in 0.66 secs
         Running `target/debug/terminal`
    thread 'main' panicked at 'failed to execute process: Os { code: 2, kind: NotFound, message: "No such file or directory" }', libcore/result.rs:945:5
    note: Run with `RUST_BACKTRACE=1` for a backtrace.
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   ljedrz    7 年前

    并非所有可用于shell的命令都可以使用 process::Command ; compgen 是一场狂欢 builtin command 并且不能在它之外工作,因为它不是一个独立的程序-它嵌入在shell中。

    但是,可以调用bash并执行 康普根 通过以下步骤 option 收件人:

    Command::new("bash").args(&["-c", "compgen"])
    
    推荐文章