How to retrieve output of external program executed from Haskell?

haskell, io

Solution

Use Shelly, a module for shell-like programming in Haskell:

http://hackage.haskell.org/package/shelly-1.4.1/docs/Shelly.html

Problem

I want to run an external program from Haskell and retrieve the contents of its output and error streams. In one of the libraries I found this code: ``` runProcess :: FilePath -> [String] -> IO (ExitCode, String, String) runProcess prog args = do (_,o,e,p) <- runInteractiveProcess prog args Nothing Nothing hSetBuffering o NoBuffering hSetBuffering e NoBuffering sout <- hGetContents o serr <- hGetContents e ecode <- length sout `seq` waitForProcess p return (ecode, sout, serr) ``` Is this the right way to do it? There are some things I don't understand here: why streams are set to `NoBuffering`? Why `length sout `seq``? This feels like some kind of hack. Also, I would like to merge output and error streams into one to get the same effect as if I did `2>&1` on the command line. If possible, I want to avoid using dedicated I/O libraries and rely on standard packages provided with GHC.

Original source