如何在D中进行系统调用并读取标准输出? [英] How to make a system call and read the stdout, in D?
问题描述
我想尝试将 D 用于一些需要高性能的系统管理脚本(用于比较性能)使用python / perl等)。
I thought to try using D for some system administration scripts which require high performance (for comparing performance with python/perl etc).
到目前为止,在我看过的教程(dsource.org等)中找不到一个示例。
I can't find an example in the tutorials I looked through so far (dsource.org etc.) on how to make a system call (i.e. calling another software) and receiving it's output from stdout, though?
如果我错过了它,有人可以将我指向正确的文档/教程,或者提供它吗?
If I missed it, could someone point me to the right docs/tutorial, or provide with the answer right away?
推荐答案
好吧,那我当然找到了: http://www.digitalmars.com/d/2.0/phobos/std_process.html#shell (使用此处的探戈库: http://www.dsource.org/projects/tango/wiki/ TutExec )。
Well, then I of course found it: http://www.digitalmars.com/d/2.0/phobos/std_process.html#shell (Version using the Tango library here: http://www.dsource.org/projects/tango/wiki/TutExec).
以前的版本s是与D 2.0兼容的版本(因此ubuntu附带了当前的dmd编译器)。
The former version is the one that works with D 2.0 (thereby the current dmd compiler that comes with ubuntu).
我现在使用dmd编译了这个小例子:
I got this tiny example to work now, compiled with dmd:
import std.stdio;
import std.process;
void main() {
string output = shell("ls -l");
write(output);
}
这篇关于如何在D中进行系统调用并读取标准输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!