Thread overview
very short pipeShell program
Jun 23, 2014
WhatMeWorry
Jun 23, 2014
Ali Çehreli
Jun 23, 2014
John Carter
June 23, 2014
After hours of reading (obviously not comprehending) std.process and looking at code samples, I still can't even do something this simple. Open a Windows command line and run miscellaneous commands. Only the first command, dir" is shown in the final output.

auto pipe = pipeShell("dir", Redirect.all);

pipe.stdin.writeln("cd");
pipe.stdin.writeln("whomai");
pipe.stdin.flush();
pipe.stdin.close();
		
foreach(str; pipe.stdout.byLine)
    writefln("from shell: %s",str);


I tried putting the wait() command was well in various places. to no avail.


June 23, 2014
On 06/22/2014 05:01 PM, WhatMeWorry wrote:
>
> After hours of reading (obviously not comprehending) std.process and
> looking at code samples, I still can't even do something this simple.
> Open a Windows command line and run miscellaneous commands. Only the
> first command, dir" is shown in the final output.
>
> auto pipe = pipeShell("dir", Redirect.all);
>
> pipe.stdin.writeln("cd");
> pipe.stdin.writeln("whomai");

Typo: whoami

> pipe.stdin.flush();
> pipe.stdin.close();
>
> foreach(str; pipe.stdout.byLine)
>      writefln("from shell: %s",str);
>
>
> I tried putting the wait() command was well in various places. to no avail.
>
>

As I understand it, the returned 'pipe' is used to communicate with the command passed to pipeShell. Since 'dir' does not understand 'cd', 'whoami', etc. it fails for you.

I tried the following on Linux and it worked. I think you must replace "bash" with "cmd" on Windows:

import std.stdio;
import std.process;

void main()
{
    auto pipe = pipeShell("bash", Redirect.all);

    pipe.stdin.writeln("dir");
    pipe.stdin.writeln("cd");
    pipe.stdin.writeln("whoami");
    pipe.stdin.flush();
    pipe.stdin.close();

    foreach(str; pipe.stdout.byLine)
        writefln("from shell: %s",str);
}

Ali

June 23, 2014
Ali, of course, is right. The only thing I'd add is for a Windowsy programmer (unless you have cygwin installed) you probably want something like "cmd.exe" instead of bash.