Binding the result of a system command to a variable in Haskell

前端 未结 2 1117
轻奢々
轻奢々 2021-02-10 11:02

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

2条回答
  •  时光取名叫无心
    2021-02-10 11:51

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

提交回复
热议问题