【问题标题】:How to read data from forked processes in Haskell?如何从 Haskell 中的分叉进程中读取数据?
【发布时间】:2011-08-15 15:04:03
【问题描述】:

谁能给我一个简短的例子,我调用一些系统命令,然后用 haskell 和 e.g. 读出它。打印出来?

我知道我可以使用 System.Cmd 来制作系统命令,例如:nm、ls、mkdir 等。

但我不需要只调用它们,我还需要读取它并使用读取的字符串进行一些操作

【问题讨论】:

    标签: haskell process system


    【解决方案1】:

    要使用的密钥库是the process package,它提供了System.Process

    调用命令并获取其输出:

    readProcess
          :: FilePath   -- command to run
          -> [String]   -- any arguments
          -> String         -- standard input
          -> IO String  -- stdout
    

    像这样:

    import System.Process
    
    main = do
        s <- readProcess "/bin/date" [] []
        putStrLn $ "The date is " ++ s
    

    运行如下:

    The date is Fri Apr 29 09:29:29 PDT 2011
    

    【讨论】:

      【解决方案2】:

      System.Process 有你想要的功能,特别是readProcess

      main = do
        wcOut <- readProcess "wc" ["/usr/share/dict"] []
        let numLines = read (head (words wcOut)) :: Int
        if numLines > 10 then return () else print "That's a small dictionary."
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-07-06
        • 2018-03-11
        • 2011-12-21
        • 2010-11-07
        • 1970-01-01
        • 2020-01-26
        • 2022-01-05
        相关资源
        最近更新 更多