Re: NSTask - grep
Re: NSTask - grep
- Subject: Re: NSTask - grep
- From: Jean-François Veillette <email@hidden>
- Date: Mon, 9 Jul 2001 20:12:49 -0400
if you want the shell to do it's work, create a task on
'/bin/tcsh -c "/usr/bin/grep somestring /Users/kglenn/*"'
That way, tcsh should expand the wildcard(*) for you.
- jfv
Le Lundi 9 juillet 2001, ` 07:19, Andreas Monitzer a icrit :
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.
_______________________________________________
cocoa-dev mailing list
email@hidden
http://www.lists.apple.com/mailman/listinfo/cocoa-dev