简体   繁体   English

是否有比`std :: process :: Command`更低级别的接口来将单个字符串作为进程执行?

[英]Is there a lower level interface than `std::process::Command` to execute a single string as a process?

Is there an equivalent in Rust for something like: Rust是否有类似的东西:

os.run("/bin/bash ln -s /dir1 /dir2");

I want the ability to spawn a process using a string expression similar to one I would use in a ptty. 我希望能够使用类似于在ptty中使用的字符串表达式生成进程。

The only utility I've found for doing this is std::process::Command , but it's not well documented and seems too complex and hard to debug. 我发现用于执行此操作的唯一实用程序是std::process::Command ,但是该文档没有充分记录,似乎过于复杂且难以调试。

There is In Rust, how do I invoke a system command and capture its output? 在Rust中,我如何调用系统命令并捕获其输出? that gives std::process::Command as an answer, hence why I specified finding it as a possible option. 给出了std::process::Command作为答案,因此为什么我指定将其作为可能的选项。 However, I'm looking for a lower level interface to make std::process::Command execute a single string as a query rather than forcing me to feed it the arguments one by one. 但是,我正在寻找一个较低级的接口来使std::process::Command作为查询执行单个字符串,而不是强迫我将参数逐个输入。

No, there is no such thing available in the standard library. 不,标准库中没有这样的东西。 As mentioned in the comments, Command is the lower level interface. 如注释中所述, Command 较低级别的界面。 Shells and other tools that take a single string have to do exceedingly clever parsing of the string in order to split it back up into pieces. 使用单个字符串的外壳程序和其他工具必须对字符串进行非常聪明的解析,才能将其拆分成碎片。 This parsing is non-trivial and may differ between shells! 这种解析是不平凡的,并且在外壳之间可能有所不同!

You can always write your own simplified parser, of course: 当然,您始终可以编写自己的简化解析器:

use std::process::Command;

fn main() {
    let args = "ls -l /etc /tmp";
    let args: Vec<_> = args.split(" ").collect();

    let output = Command::new(args[0])
        .args(&args[1..])
        .output()
        .expect("Couldn't run it");

    println!("{:?}", ::std::str::from_utf8(&output.stdout));
    println!("{:?}", ::std::str::from_utf8(&output.stderr));
}

but it's not well documented 但是没有很好的记录

I'm obviously biased, but I don't understand how it could be more documented. 我显然有失偏颇,但我不明白它如何能够多有记载。 Every useful method has prose describing it and examples demonstrating it. 每种有用的方法都有散文来描述它,并举例说明了它。

and seems too complex 似乎太复杂了

I'm not sure why; 我不知道为什么; perhaps the amount of choice is overwhelming, but realistically that control is needed. 选择的数量也许是压倒性的,但实际上需要控制。

and hard to debug. 而且很难调试。

I'm not sure what debugging needs to be done — either the code will compile or not, then it will execute or not. 我不确定需要进行什么调试-代码是否会编译,然后是否执行。 Compiler errors are frequently useful, runtime errors of this nature usually depend on the OS. 编译器错误经常有用,这种性质的运行时错误通常取决于操作系统。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM