I'm trying to write some code which runs grep
externally, then analyses the output. Specifically, I want to do
grep <username> *.test
but, sadly,
readProcess "grep" [username, "*.test"]
seems to generate the command with double quotes around the arguments
grep "<username>" "*.test"
and as there is no individual file called asterisk-dot-test, grep
barfs. There are files with .test
extensions.
Can I persuade readProcess
(or something similar) to issue the command I want?
"*" is expanded not by grep
, but by shell. You should run something like sh -c 'grep username *.test
if you want the expansion.
A better way is to use createProcess
with ShellCommand
argument.
You're probably best going to createProcess
, which is the most general process creation function. Something like...
import System.Process
import System.IO
makeGrep username file = "grep " ++ username ++ " " ++ file
main :: IO ()
main = do
(_, Just hOut, _, hProc) <- createProcess (
(shell (makeGrep "bob" "*.test"))
{ std_out = CreatePipe }
)
exitCode <- waitForProcess hProc
output <- hGetContents hOut
print output
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With