如何从 Haskell 中的分叉进程读取数据?

发布于 2024-11-03 19:26:25 字数 149 浏览 0 评论 0原文

谁可以给我一个简短的例子,我调用一些系统命令,然后用 haskell 读出它,例如打印它?

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

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

who can give me a short example where i call some system command and then read it out with haskell and e.g. print it?

i know that i can use System.Cmd to make system commands like: nm, ls, mkdir etc.

but i dont need to call them only also i need to read it and make some operations with the readed string

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

同展鸳鸯锦 2024-11-10 19:26:25

要使用的关键库是 process,它提供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

The key library to use is the process package, which provides System.Process.

To call a command and get its output:

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

Like so:

import System.Process

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

which runs as:

The date is Fri Apr 29 09:29:29 PDT 2011
辞慾 2024-11-10 19:26:25

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."

System.Process has the functions you want, specifically 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."
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文