问题
The following snippet of code executes a grep
command and binds the output to stdout'
, stderr'
and errCode
respectively.
main :: IO ()
main = do
let stdin' = ""
(errCode, stdout', stderr') <- readProcessWithExitCode "grep" ["search-term" ,"-nr", "/path/to/be/searched"] stdin'
putStrLn $ "stdout: " ++ stdout'
putStrLn $ "stderr: " ++ stderr'
putStrLn $ "errCode: " ++ show errCode
The problem is that stdout'
only captures the first result of the search. I think this is because grep
is something akin to a spawned process that feeds search results one file by one file until it is finished.
Question: I need the entire output of the standard output of grep to be binded to stdout'
. Is this possible in Haskell, and if so, what is an idiomatic way to do it?
EDIT: It turns out that the issue wasn't as I thought it was. Namely, I had only one file in my directory that wasn't symlinked. I forgot to use -R
as an option with grep
, and so those symlinks weren't being traversed in the search! The issue has been resolved.
来源:https://stackoverflow.com/questions/37197388/binding-the-entire-output-of-an-ongoing-system-process-to-a-variable-in-haskell