[英]How do I execute a command in a subshell in Rust?
In Python, I could do os.system("pip install bs4")
.在 Python 中,我可以执行os.system("pip install bs4")
。 Is there any equivalent in Rust? Rust 中是否有任何等价物? I've seen std::process::Command
, but this seems to fail each time:我见过std::process::Command
,但这似乎每次都失败:
use std::process::Command;
Command::new("pip")
.arg("install")
.arg("bs4")
.spawn()
.expect("pip failed");
Is there any way to have the code execute a real shell and have them run in the terminal?有没有办法让代码执行一个真正的 shell 并让它们在终端中运行?
Pip requires root permissions so be sure to run your binary with sufficient privileges. Pip 需要 root 权限,因此请确保以足够的权限运行二进制文件。
The following worked for me:以下对我有用:
use std::process::Command;
Command::new("pip")
.args(&["install", "bs4"])
.spawn()
.expect("failed to execute process");
Use this to analyze the failure:使用它来分析故障:
use std::process::Command;
let output = Command::new("pip")
.args(&["install", "bs4"])
.output()
.expect("failed to execute process");
println!("status: {}", output.status);
println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
Example was derived from here:示例源自此处:
How do I invoke a system command in Rust and capture its output? 如何在 Rust 中调用系统命令并捕获其输出?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.