[Haskell-cafe] Execution of external command

Duncan Coutts duncan.coutts at worc.ox.ac.uk
Thu Dec 13 08:43:17 EST 2007

On Thu, 2007-12-13 at 16:27 +0300, Bulat Ziganshin wrote:
> Hello Duncan,
> Thursday, December 13, 2007, 4:10:26 PM, you wrote:
> >> i need to run external command with parameter and get its stdout, smth
> > temporary file. It seems it is not possible to use pipes to get the
> > stdout and have the resulting code be portable between Haskell
> > implementations (it's a favourite peeve of mine).
> i don't need to interact with program, just get its whole output (one
> line) after it was finished. btw, afair, there was problems in windows
> with redirection of cmd.exe output (in particular when running
> executables from command files)
> separate windows and unix versions are ok for me, i just need them
> both. and i need only ghc support
> taking this all into account, where i should look?

Use just the GHC bit from the code I pointed at:

  bracket (liftM2 (,) (openTempFile tmpDir "cmdstdout") (openFile devNull WriteMode))
          -- We need to close tmpHandle or the file removal fails on Windows
          (\((tmpName, tmpHandle), nullHandle) -> do
             hClose tmpHandle
             removeFile tmpName
             hClose nullHandle)
         $ \((tmpName, tmpHandle), nullHandle) -> do
    cmdHandle <- runProcess path args Nothing Nothing
                   Nothing (Just tmpHandle) (Just nullHandle)
    exitCode <- waitForProcess cmdHandle
    output <- readFile tmpName
    evaluate (length output)
    return (output, exitCode)

More information about the Haskell-Cafe mailing list