如何调用系统命令并捕获其输出? [英] How do I invoke a system command and capture its output?

查看:43
本文介绍了如何调用系统命令并捕获其输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法调用系统命令,比如 Rust 中的 lsfuser?捕获它的输出怎么样?

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:

  • spawn — 运行程序并返回一个包含详细信息的值
  • 输出 — 运行程序并返回输出
  • status — 运行程序并返回退出代码

文档中的一个简单示例:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆