sam46
sam46

Reputation: 1271

Pipe string to shell command in Haskell

How do I feed a string to shell command and get the output it produces in Haskell?

For example, given this:

> myHaskellProg 
  "blah"

> myHaskellProg | wc
  4

I want

> myHaskellProg

to print 4 by calling wc and printing out the result

I was able to call shell commands using process's callCommand but how do I pipe a string to it and get back the result or stderr?

Upvotes: 2

Views: 516

Answers (1)

Thomas M. DuBuisson
Thomas M. DuBuisson

Reputation: 64740

You are looking for System.Process and you can use the shell function to create a description of a process then use readCreateProcess to run the shell command, provide stdin and read stdout.

import System.Process
main :: IO ()
main =
  do result <- readCreateProcess (shell "ls") myHaskellString
     putStrLn result

myHaskellString :: String
myHaskellString = "string"

Upvotes: 5

Related Questions