Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I make readProcess drop the quotes?

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?

like image 496
pigworker Avatar asked Oct 31 '13 10:10

pigworker


2 Answers

"*" 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.

like image 108
tempestadept Avatar answered Oct 22 '22 00:10

tempestadept


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
like image 1
Adam Wright Avatar answered Oct 21 '22 23:10

Adam Wright