命令 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/