如何调用系统命令并捕获其输出? [英] How do I invoke a system command and capture its output?
本文介绍了如何调用系统命令并捕获其输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法调用系统命令,比如 Rust 中的 ls
或 fuser
?捕获它的输出怎么样?
Is there a way to invoke a system command, like ls
or fuser
in Rust? How about capturing its output?
推荐答案
std::process::Command
允许这样做.
std::process::Command
allows for that.
有多种方法可以生成子进程并在机器上执行任意命令:
There are multiple ways to spawn a child process and execute an arbitrary command on the machine:
文档中的一个简单示例:
One simple example from the docs:
use std::process::Command;
Command::new("ls")
.arg("-l")
.arg("-a")
.spawn()
.expect("ls command failed to start");
这篇关于如何调用系统命令并捕获其输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文