Re: NSTask - grep
Re: NSTask - grep
- Subject: Re: NSTask - grep
- From: Andreas Monitzer <email@hidden>
- Date: Tue, 10 Jul 2001 01:19:12 +0200
On Tuesday, July 10, 2001, at 12:00 , Kent Glenn wrote:
I'm having some problems getting grep to work with NSTask. I've gotten
NSTask to work with some of the examples for "ls" on this list. I have
even gotten it to work partially with "grep".
I want to grep for a pattern in a directory, and it's sub-directories. If
I use a file name instead of the wildcard '*', then it will work. When I
try to use the wildcard, I get this message: "grep: /Users/kglenn/*: No
such file or directory". If I execute the same command from Terminal, it
works perfectly. I thought maybe I had to escape the '*' at first, but
that didn't work.
NSTask *myTask=[[NSTask alloc] init];
[myTask setLaunchPath:@"/usr/bin/grep"];
[myTask setArguments:[NSArray
arrayWithObjects:@"-r",@"somestring",@"/Users/kglenn/*",nil]];
[myTask launch];
[myTask release];
I know it's something stupid I'm doing. Thanks in advance for the help.
The shell expands '*' before sending the command to grep. That means you
have to expand it yourself for grep (using NSDirectoryEnumerator):
NSDirectoryEnumerator *direnum = [[NSFileManager defaultManager]
enumeratorAtPath:@"/Sales/Reports"];
NSMutableArray *arguments=[[NSMutableArray alloc] initWithObjects:@"-r",@"
somestring",nil];
NSTask *myTask=[[NSTask alloc] init];
NSString *pname;
while (pname = [direnum nextObject]) {
[arguments addObject:pname];
}
[myTask setLaunchPath:@"/usr/bin/grep"];
[myTask setArguments:arguments];
[myTask launch];
[myTask release];
[arguments release];
btw there might be a more efficient way (a C-API instead of grep).
andy
--
Discussion forthcoming.