D:Windows上的executeShell运行另一个不立即返回的程序

时间:2014-02-03 22:36:47

标签: windows shell d

我正在使用D作为Windows 7控制台内容的脚本语言来自动执行枯燥的任务。我的一个脚本(open.exe)应该允许我从命令行打开东西而不必指定我使用的程序(我有一个带有这个东西的配置文件)。现在,我使用executeShell来执行此操作,并调用start [name of program I want to use] [name of input file]之类的内容。如果我直接从shell执行此操作,它会立即返回,但如果我使用我的D脚本执行此操作,则在它打开的程序关闭之前不会返回。我该怎么办才能让它立即归还?

出于参考目的,这是我的脚本的业务逻辑(main方法只是为了管道目的而进行一些参数解析):

immutable path = "some//path//going//to//config//file.conf";

void process(string input) {
string extension = split(input,".")[1]; //get file extension from input
auto config = File(path,"r"); auto found = false;
while (!config.eof()){
    auto line = chomp(config.readln());
    if (line[0]!='#') { //skip comment lines
        auto divided = split(line, ":");
        if (divided[0] == extension) {
            found = true;
            auto command = "start " ~ divided[1] ~ " " ~ input;
            auto result = executeShell(command);
            //test for error code and output if necessary
            writeln(result.output);
        }
    }
}
if (!found)
    writeln("ERROR: Don't know how to open " ~ input);

}

1 个答案:

答案 0 :(得分:4)

the std.process documentation的顶部开始:

  

执行并等待完成,收集输出 - executeShell

Windows start程序会生成一个进程并立即退出。 D's executeShell做了别的事情。如果您想要生成另一个程序,请使用相应的函数:spawnProcessspawnShell