Haskell System.Process Handing

时间:2015-09-28 23:15:39

标签: linux haskell

我希望从Linux命令fortune -a -s获取输出并输入irc通道。我必须加载System.Process才能访问。

rawSystem("fortune" ["-a", "-s"])

返回退出代码和幸运字符串,两者都是我要捕获的。

我知道我需要使用元组,但不断出错。

(status, fortune) <- rawSystem("fortune" ["-a", "-s"])

这样我就可以处理退出代码(状态)和财富(字符串)。

1 个答案:

答案 0 :(得分:1)

根据Hackage,rawSystem returns type IO ExitCode,但是readProcess does basically what you want。能够在ghci中使用此功能:

$ ghci
Prelude> :m System.Process
Prelude System.Process> ec <- rawSystem "fortune" [ "-a", "-s" ]
There once was a girl from Spokane,
Went to bed with a one-legged man.
    She said, "I know you--
    You've really got two!
Why didn't you say so when we began?"
Prelude System.Process> :t ec
ec :: GHC.IO.Exception.ExitCode
Prelude System.Process> out <- readProcess "fortune" [ "-a", "-s" ] ""
Prelude System.Process> :t out
out :: String
Prelude System.Process> out
"The truth about a woman often lasts longer than the woman is true.\n"