very short pipeShell program

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jun 22 17:55:52 PDT 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



More information about the Digitalmars-d-learn mailing list