www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - std.process spawnShell/pipeShell dont capture output of the shell

reply "Colin Grogan" <grogan.colin gmail.com> writes:
Hi folks,

Is there anyway to make std.process.spawnShell or 
std.process.pipeShell capture the output of the shell interface?

For example, the code:

import std.stdio;
import std.process;
void main(string[] args){

     writefln("Executing: %s", args[1]);
     auto processPipes = pipeShell(args[1], Redirect.all);
		
     foreach(str; processPipes.stdout.byLine){
	writefln("STDOUT: %s",str);
     }
	
     foreach(str; processPipes.stderr.byLine){
	writefln("STDERR: %s",str);
     }	
}

is compiled to an executable called "mess".
When executed with the following produces:
"
~/test$ ./mess ls
Executing: ls
STDOUT: mess
STDOUT: text.txt

"
Thats all fine, however, I'd expect it to print another "~/test$" 
at the end, as if its an interactive shell waiting for input.

Is that an incorrect assumption to make or am I doing something 
wrong that is stopping that from happening?


Also, If I run /bin/bash through program, it hangs.

"
~/test$ ./mess /bin/bash
Executing: /bin/bash

"

Thanks,
Colin
Oct 09 2013
parent reply "Jesse Phillips" <Jesse.K.Phillips+D gmail.com> writes:
On Wednesday, 9 October 2013 at 11:22:26 UTC, Colin Grogan wrote:
 "
 ~/test$ ./mess ls
 Executing: ls
 STDOUT: mess
 STDOUT: text.txt

 "
 Thats all fine, however, I'd expect it to print another 
 "~/test$" at the end, as if its an interactive shell waiting 
 for input.
It is extracting the output of the program, 99.9% of the time this is what everyone wants and including the shells behavior would be terrible. If you run bash, then it is going to be waiting for stdin so you'd need to pass it "ls" and "exit" to get the output and quit. I'm not sure this would get you want you wanted.
Oct 09 2013
parent reply "Colin Grogan" <grogan.colin gmail.com> writes:
On Wednesday, 9 October 2013 at 14:27:30 UTC, Jesse Phillips 
wrote:
 On Wednesday, 9 October 2013 at 11:22:26 UTC, Colin Grogan 
 wrote:
 "
 ~/test$ ./mess ls
 Executing: ls
 STDOUT: mess
 STDOUT: text.txt

 "
 Thats all fine, however, I'd expect it to print another 
 "~/test$" at the end, as if its an interactive shell waiting 
 for input.
It is extracting the output of the program, 99.9% of the time this is what everyone wants and including the shells behavior would be terrible.
But its exactly what I want ;)
 If you run bash, then it is going to be waiting for stdin so 
 you'd need to pass it "ls" and "exit" to get the output and 
 quit. I'm not sure this would get you want you wanted.
That would imply that the call to pipeShell("/bin/bash", Redirect.all); is blocking. However, its not meant to be blocking is it not? That new /bin/bash process is meant to run in parallel to the main process? If I use the program to execute the following script: #!/bin/bash for i in {1..100} do for j in {1..100} do echo "$i, $j" done sleep 15 done and monitor the output that is printed, I can see that it is indeed sleeping for 15 seconds, so the pipeShell call is definitely non-blocking. I assumed it should behave the same way for /bin/bash. It is just another process after all, surely pipeShell knows nothing more about /bin/bash that it does about my silly script above?
Oct 09 2013
parent reply "Jesse Phillips" <Jesse.K.Phillips+D gmail.com> writes:
On Wednesday, 9 October 2013 at 14:54:32 UTC, Colin Grogan wrote:
 is blocking. However, its not meant to be blocking is it not? 
 That new /bin/bash process is meant to run in parallel to the 
 main process?
I'm not sure exactly the implementation. But if you're asking to run bash and then print its output, wouldn't it have to block because the output won't be complete until the program has finished running? And since bash will run until you exit the program will block for ever.
Oct 09 2013
parent "Colin Grogan" <grogan.colin gmail.com> writes:
On Thursday, 10 October 2013 at 01:24:03 UTC, Jesse Phillips 
wrote:
 On Wednesday, 9 October 2013 at 14:54:32 UTC, Colin Grogan 
 wrote:
 is blocking. However, its not meant to be blocking is it not? 
 That new /bin/bash process is meant to run in parallel to the 
 main process?
I'm not sure exactly the implementation. But if you're asking to run bash and then print its output, wouldn't it have to block because the output won't be complete until the program has finished running? And since bash will run until you exit the program will block for ever.
From what I understood in the documentation, I didnt think it should block. I understood it's meant to run the child process in the background and you communicate with it via the stdin/stdout/stderr pipes. But, from experimentation I can see that running /bin/bash does appear to block. Ill have to do some more digging when I have the time...
Oct 11 2013