[Haskell-cafe] Re: Trouble with simple embedding of shell in haskell

Donn Cave donn at drizzle.com
Fri Sep 22 01:09:29 EDT 2006


On Thu, 21 Sep 2006, George Brewster wrote:
...
> Yup, I'm on linux. My goal is to have a function which gives me a lazy 
> string which is the output of a shell command, and takes a string as 
> input. In particular, I'd like the shell command to behave lazily (read 
> only as much input as is needed, and write only as much output as 
> needed), so things like this work (should terminate and print a few 
> lines of "hi"):
> 
> main = sh "yes hi" "there" >>= sh "head" >>= putStrLn

Good luck!

> I tried your suggestion out (remove forkIO), and when I do that, this 
> example returns but doesn't print anything.

For me, it hangs - since I left the wait in.  If I omit the wait, it's
"broken pipe", I think because we're trying to write data to "head"
after it exits.

	Donn Cave, donn at drizzle.com

> sh :: String -> String -> IO String
> sh cmd = \input ->
>           do (stdin, stdout, _, pid) <- runInteractiveCommand cmd
>              forkIO $ hPutStr stdin input >> hClose stdin
>              forkIO $ waitForProcess pid >> return ()
>              hGetContents stdout



More information about the Haskell-Cafe mailing list