Binding the result of a system command to a variable in Haskell

前端 未结 2 1082
轻奢々
轻奢々 2021-02-10 11:02

How does one run a system command in Haskell and bind it\'s result (i.e., standard output) to a variable? In pseudo-haskell I\'m looking for something like the

2条回答
  •  遇见更好的自我
    2021-02-10 11:40

    Expanding on jamshidh's answer, if instead of getting stdout and stderr as String you want to get them as ByteString or Text, you can use the process-extras package.

    You can also use my process-streaming package:

    $ import System.Process.Streaming
    $ execute (piped (shell "echo foo")) (liftA3 (,,) (foldOut intoLazyBytes)    
                                                      (foldErr intoLazyBytes)
                                                      exitCode)
    ("foo\n","",ExitSuccess)
    

    Also for Text:

    $ import System.Process.Streaming.Text
    $ execute (piped (shell "echo foo")) (liftA3 (,,) (foldOut (transduce1 utf8x intoLazyText)) 
                                                      (foldErr (transduce1 utf8x intoLazyText)) 
                                                      exitCode)
    ("foo\n","",ExitSuccess)
    

提交回复
热议问题