[Haskell-cafe] Using external programs with Haskell in windows.

Donald Bruce Stewart dons at cse.unsw.edu.au
Sat Mar 17 22:13:57 EDT 2007

> I'm looking for a way to run an external program and get the results in 
> haskell. Something similar to HSH but that will work in windows. I don't 
> need anything too complex, just to provide the command to be run as a 
> string and get the result as a string. Is this possible?
> One of the HSH examples is all I need:
> runS $ "echo /etc/pass*"
> -> "/etc/passwd /etc/passwd-"

System.Process is your friend. E.g.

    import System.Process
    import Control.Exception
    import System.IO
    import System.Exit

    -- Run a normal shell command
    run :: String -> IO String
    run s = handle (fail . show) $ do
        (ih,oh,eh,pid) <- runInteractiveCommand s
        so <- hGetContents oh
        se <- hGetContents eh
        hClose ih
        ex <- waitForProcess pid
        case ex of
            ExitFailure e      -> fail $ "Failed with status: " ++ show e
            _  | not (null se) -> fail se
               | otherwise     -> return so

*Process> run $ "echo *.hs"
"Main.hs Process.hs Shell.hs\n"

-- Don

More information about the Haskell-Cafe mailing list