Call external program and get stdout

Hi, How can I call a program (like, for instance, 'grep text *') and get the standard output? All actions I found (executeFile, system) do not give me the output of the program. Thanks, Maurício

Maurício wrote:
Hi,
How can I call a program (like, for instance, 'grep text *') and get the standard output? All actions I found (executeFile, system) do not give me the output of the program.
http://haskell.org/ghc/docs/latest/html/libraries/process-1.0.0.0/System-Pro...

Jules Bean wrote:
Maurício wrote:
Hi,
How can I call a program (like, for instance, 'grep text *') and get the standard output? All actions I found (executeFile, system) do not give me the output of the program.
http://haskell.org/ghc/docs/latest/html/libraries/process-1.0.0.0/System-Pro...
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
I was using the following: {- A small function for blindly running a process until it completes its output and then waiting for its exit code. We return both the output (excluding stderr) plus the exit code. -} getProcessOutput :: String -> IO (String, ExitCode) getProcessOutput command = -- Create the process do (_pIn, pOut, pErr, handle) <- runInteractiveCommand command -- Wait for the process to finish and store its exit code exitCode <- waitForProcess handle -- Get the standard output. output <- hGetContents pOut -- return both the output and the exit code. return (output, exitCode) You'll need the following imports: import System.IO ( hGetContents ) import System.Process ( runInteractiveCommand , waitForProcess ) import System.Exit ( ExitCode ( .. ) ) regards allan

Hi,
How can I call a program (like, for instance, 'grep text *') and get the standard output? (...)
http://haskell.org/ghc/docs/latest/html/libraries/process-1.0.0.0/System-Pro...
I was using the following: (...) do (_pIn, pOut, pErr, handle) <- runInteractiveCommand command -- Wait for the process to finish and store its exit code exitCode <- waitForProcess handle (...)
I also used runInteractiveCommand, which I found in the page quoted above. I read the output handle without waiting for the process to finish, and I thought runInteractiveCommand would actually not return before the external process finishes, which your code showed me to be a wrong prejudice. That's nice. In my situation, the output of the program can be really big, so I prefer not to wait for it to finish before I do stuff with the output, but I'll probably use your code in most situations, since I can check the exit code before doing anything. Best, Maurício

Allan Clark wrote:
-- Create the process do (_pIn, pOut, pErr, handle) <- runInteractiveCommand command -- Wait for the process to finish and store its exit code exitCode <- waitForProcess handle
Warning: this will get stuck if the command output is so big that it fills the SO buffers. This limit is 64K here. Regards, Zun.

Roberto Zunino wrote:
Allan Clark wrote:
-- Create the process do (_pIn, pOut, pErr, handle) <- runInteractiveCommand command -- Wait for the process to finish and store its exit code exitCode <- waitForProcess handle
Warning: this will get stuck if the command output is so big that it fills the SO buffers. This limit is 64K here.
Regards, Zun.
Ah thanks for the warning, I've never tested this on anything particularly large. regards allan

dons did a blog post about a "shell monad" which I think does what you ask.
http://cgi.cse.unsw.edu.au/~dons/blog/2007/03
very nice, I use it myself.
t.
2007/11/22, Maurício
Hi,
How can I call a program (like, for instance, 'grep text *') and get the standard output? All actions I found (executeFile, system) do not give me the output of the program.
Thanks, Maurício
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (5)
-
Allan Clark
-
Jules Bean
-
Maurício
-
Roberto Zunino
-
Thomas Hartman