www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - very short pipeShell program

reply "WhatMeWorry" <kc_heaser yahoo.com> writes:
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.
Jun 22 2014
next sibling parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
Jun 22 2014
prev sibling parent "John Carter" <john.carter taitradio.com> writes:
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.
Jun 23 2014