【发布时间】:2011-08-15 15:04:03
【问题描述】:
谁能给我一个简短的例子,我调用一些系统命令,然后用 haskell 和 e.g. 读出它。打印出来?
我知道我可以使用 System.Cmd 来制作系统命令,例如:nm、ls、mkdir 等。
但我不需要只调用它们,我还需要读取它并使用读取的字符串进行一些操作
【问题讨论】:
谁能给我一个简短的例子,我调用一些系统命令,然后用 haskell 和 e.g. 读出它。打印出来?
我知道我可以使用 System.Cmd 来制作系统命令,例如:nm、ls、mkdir 等。
但我不需要只调用它们,我还需要读取它并使用读取的字符串进行一些操作
【问题讨论】:
要使用的密钥库是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
【讨论】:
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."
【讨论】: