【问题标题】:Why does the compgen command work in the Linux terminal but not with process::Command?为什么 compgen 命令在 Linux 终端中有效,而在 process::Command 中无效?
【发布时间】:2018-04-19 19:15:30
【问题描述】:

compgen 命令在 Linux 终端中运行良好,但在使用 Command::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.

【问题讨论】:

  • 这是一个内置的shell而不是可执行文件,所以你需要调用shell。

标签: shell terminal rust


【解决方案1】:

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

但是,可以通过将以下 option 传递给 bash 并执行 compgen

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

【讨论】:

    猜你喜欢
    • 2021-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-01
    • 2021-07-18
    • 2019-05-07
    相关资源
    最近更新 更多