[Haskell-cafe] Call external program and get stdout
a.d.clark at ed.ac.uk
Thu Nov 22 14:17:08 EST 2007
Jules Bean wrote:
> Maurício wrote:
>> 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.
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
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:
( hGetContents )
( ExitCode ( .. ) )
More information about the Haskell-Cafe