D语言新手问题 Dlang 如果调用系统命令

· Created · Last modified by 龙卷锋123 replied at · 1583 times read

当我们想要使用到系统下的命令来完成功能的实现的时候,就可以使用Dlang的一个函数executeShell,这个函数需要引用std.process库;
下边的代码就是一些使用示例:

import std.process;
auto r1 = executeShell("echo foo");
assert(r1.status == 0);
assert(r1.output.chomp() == "foo");

auto r2 = executeShell("echo bar 1>&2");
assert(r2.status == 0);
assert(r2.output.chomp().stripRight() == "bar");

auto r3 = executeShell("exit 123");
assert(r3.status == 123);
assert(r3.output.empty);

auto r4 = executeShell("echo stderr test, please ignore 1>&2",null, Config.stderrPassThrough);
assert(r4.status == 0);
assert(r4.output.empty);
Login to reply