将系统命令的结果绑定到Haskell中的变量 [英] Binding the result of a system command to a variable in Haskell
问题描述
如何在Haskell 和中运行系统命令,将其结果(即标准输出)绑定到变量?在伪哈斯克尔中,我正在寻找如下内容:
import System.Process
main = do输出< - callCommandecho hi
putStrLn输出 - 结果:hi
这不起作用。是否有类似的东西?
以下是一些工作代码:
import System.Process
$ b $ main :: IO()
main = do
let stdin'=
(errCode,stdout',stderr')< - readProcessWithExitCodeecho[hi] stdin'
putStrLn $stdout:++ stdout'
putStrLn $stderr:++ stderr'
putStrLn $errCode:++ show errCode
Carsten指出,可以使用readProcess,但我更喜欢这个版本,因为任何严重的用法,你很快会对你的命令为什么会失败而感到困惑(如果你在命令行中运行,当然会显示stdout和stderr)。
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 following:
import System.Process
main = do output <- callCommand "echo hi"
putStrLn output -- result: "hi"
This does not work. Is there something similar that does?
Here is some working code:
import System.Process
main :: IO ()
main = do
let stdin' = ""
(errCode, stdout', stderr') <- readProcessWithExitCode "echo" ["hi"] stdin'
putStrLn $ "stdout: " ++ stdout'
putStrLn $ "stderr: " ++ stderr'
putStrLn $ "errCode: " ++ show errCode
As Carsten notes, you can use readProcess, but I prefer this version because with any serious usage, you will soon be very confused as to why your command is silently failing (if you run in the command line, both stdout and stderr are, of course, shown).
这篇关于将系统命令的结果绑定到Haskell中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!