[Haskell-cafe] Call external program and get stdout
Allan Clark
a.d.clark at ed.ac.uk
Thu Nov 22 14:17:08 EST 2007
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-Process.html
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at 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
More information about the Haskell-Cafe
mailing list