Re: NSTask & GREP problems
Re: NSTask & GREP problems
- Subject: Re: NSTask & GREP problems
- From: Tom Harrington <email@hidden>
- Date: Tue, 23 Aug 2005 14:16:28 -0600
On 8/22/05, Duncan Campbell <email@hidden> wrote:
> I'm having a go building a front-end to the unix "grep" command. I'm
> using the "Moriarity" sample app as a starting point and have got the
> basics working.
>
> I am, however encountering some problems passing the wildcard
> character ('*') into my grep statement.
>
> My problem is that i want to restrict the types of files I search
> within. To do this, you can execute something like the following in
> terminal:
>
> grep -cr --include='*[.txt|.rtf]' 'texttosearchfor' /path/to/search/
> within/
>
> which would restrict searching to files with the .txt and .rtf
> extensions.
The problem you're running into here is the lack of a shell. The
wildcard character is a shell metacharacter, and when you grep from
the command line, the shell interprets it. But when you run from
NSTask, there's no shell, and thus the asterisk is treated literally
without interpretation or expansion. I suspect you actually have
multiple problems related to the square brackets, but for the same
reason.
One way around this is to get a shell into the mix.
In the example you found in the archives, the author had attempted to
do something like this:
[myTask setLaunchPath:@"/usr/bin/grep"];
[myTask setArguments:[NSArray
arrayWithObjects:@"-r",@"private",@"/private/tmp/*",nil]];
(I changed the paths and strings, but the functionality is identical).
The fails because there's no directory called "/private/tmp/*" (with
the "*" treated literally rather than interpreted as a wildcard). A
fix to this is to rewrite it as follows:
[myTask setLaunchPath:@"/bin/sh"];
[myTask setArguments:[NSArray
arrayWithObjects:@"-c", @"/usr/bin/grep -r private
/private/tmp/*",nil]];
This version explicitly calls /bin/sh, which will handle the asterisk
just as it would be handled at the command line (and note that sh's
"-c" is important here). This makes the NSTask work more like your
command line. Putting the actual command into a single NSString seems
a little awkward compared with using an array, but you could always
build the string from some other array before kicking off the NSTask.
--
Tom Harrington
email@hidden
AIM: atomicbird1
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden