shell - 为什么 compgen 命令在 Linux 终端中有效,但在 process::Command 中无效?

标签 shell terminal rust

命令 compgen 在 Linux 终端中运行良好,但在使用 Command::new 时会导致 panic 。

我的代码:

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 的命令都可以使用 process::Command 运行; compgen是一个 bash builtin command并且在它之外不起作用,因为它不是一个独立的程序——它嵌入在 shell 中。

但是,可以通过传递以下 option 来调用 bash 并执行 compgen对它:

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

关于shell - 为什么 compgen 命令在 Linux 终端中有效,但在 process::Command 中无效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49928601/

相关文章:

rust - Rust 中函数的类型同义词

linux - Arch Linux 的 Gnome 终端不工作

shell - 缩进终端/shell 输出

python - 使用子进程从 python 脚本编译 C++ 程序

linux - 在 linux 中使用自定义别名中的 find 命令没有得到结果

c - 如何在 unix/posix 中清除(重置)屏幕? (不是诅咒/换行符)

rust - 如何构建在WSL上使用device_query crate 的项目?

rust - 使用泛型类型时, `From`的实现有何冲突?

linux - awk 仅当行以结果开头时才匹配

Linux 一个带管道操作的长shell命令