如何在D中进行系统调用并读取标准输出?

Sam*_*mpa 7 d system-calls

我想尝试将D用于一些需要高性能的系统管理脚本(用于比较性能与python/perl等).

我在目前为止看过的教程(dsource.org等)中找不到一个例子,说明如何进行系统调用(即调用另一个软件)并从stdout接收它的输出?

如果我错过了,有人可以指向正确的文档/教程,或者立即提供答案吗?

Sam*_*mpa 12

那么,我当然找到了它:http://www.digitalmars.com/d/2.0/phobos/std_process.html#shell(使用Tango库的版本:http://www.dsource.org/projects/ tango/wiki/TutExec).

前一个版本是与D 2.0一起使用的版本(因此是ubuntu附带的当前dmd编译器).

我现在有了这个小例子,用dmd编译:

import std.stdio;
import std.process;

void main() {
  string output = shell("ls -l");
  write(output);
}
Run Code Online (Sandbox Code Playgroud)