本文介绍了为什么 compgen 命令在 Linux 终端中有效,而在 process::Command 中无效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
命令 compgen
在 Linux 终端中工作正常,但在使用 Command::new 时会导致恐慌.
The command compgen
works fine in the Linux Terminal, but causes a panic when using 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 的命令都可以使用 process::Command
运行;compgen
是一个 bash 内置命令并且在它,因为它不是一个独立的程序 - 它嵌入在 shell 中.
Not all commands available to the shell are runnable with process::Command
; compgen
is a bash builtin command and doesn't work outside of it, because it is not a standalone program - it is embedded within the shell.
但是,可以通过传递以下 :
It is, however, possible to invoke bash and execute compgen
by passing the following option to it:
Command::new("bash").args(&["-c", "compgen"])
这篇关于为什么 compgen 命令在 Linux 终端中有效,而在 process::Command 中无效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!