* encapulating a find function in a script
@ 2004-01-15 0:19 Jeffrey Holle
0 siblings, 0 replies; 7+ messages in thread
From: Jeffrey Holle @ 2004-01-15 0:19 UTC (permalink / raw
To: linux-console
I'm having a problem with a simple script I'm trying to develop.
Its name is fgrep and its text is:
#!/bin/bash
find $1 -name $2 -exec grep -H $3 '{}' ';'
I want to use it like "dgrep 'directory' 'file pattern' 'match text'.
It doesn't match anything because of the $2 parameter. If I replace
"$2" with "*.cpp", it works.
Can anybody tell me what is wrong and how to fix it?
^ permalink raw reply [flat|nested] 7+ messages in thread
* encapulating a find function in a script
@ 2004-01-15 0:34 BobGian
2004-01-15 1:00 ` Mike Castle
2004-01-15 1:55 ` Jeffrey Holle
0 siblings, 2 replies; 7+ messages in thread
From: BobGian @ 2004-01-15 0:34 UTC (permalink / raw
To: jeff.holle; +Cc: linux-console
> I'm having a problem with a simple script I'm trying to develop.
>
> Its name is fgrep and its text is:
>
> #!/bin/bash
> find $1 -name $2 -exec grep -H $3 '{}' ';'
>
> I want to use it like "dgrep 'directory' 'file pattern' 'match text'.
>
> It doesn't match anything because of the $2 parameter. If I replace
> "$2" with "*.cpp", it works.
>
> Can anybody tell me what is wrong and how to fix it?
Try putting double-quotes around the parameters, like:
find $1 -name "$2" -exec grep -H "$3" '{}' ';'
This works for me in a similar script, at least in the csh shell.
-Bob Giansiracusa
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: encapulating a find function in a script
2004-01-15 0:34 BobGian
@ 2004-01-15 1:00 ` Mike Castle
2004-01-15 1:55 ` Jeffrey Holle
1 sibling, 0 replies; 7+ messages in thread
From: Mike Castle @ 2004-01-15 1:00 UTC (permalink / raw
To: BobGian; +Cc: jeff.holle, linux-console
On Wed, Jan 14, 2004 at 04:34:01PM -0800, BobGian@U.Washington.edu wrote:
> Try putting double-quotes around the parameters, like:
>
> find $1 -name "$2" -exec grep -H "$3" '{}' ';'
>
> This works for me in a similar script, at least in the csh shell.
You may also find that using xargs instead of -exec is much faster (fewer
fork/execs).
Something like:
find "$1" -name "$2" -print0 | xargs -0 grep -H "$3"
mrc
--
Mike Castle dalgoda@ix.netcom.com www.netcom.com/~dalgoda/
We are all of us living in the shadow of Manhattan. -- Watchmen
fatal ("You are in a maze of twisty compiler features, all different"); -- gcc
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: encapulating a find function in a script
2004-01-15 0:34 BobGian
2004-01-15 1:00 ` Mike Castle
@ 2004-01-15 1:55 ` Jeffrey Holle
2004-01-16 21:09 ` Jeffrey Holle
1 sibling, 1 reply; 7+ messages in thread
From: Jeffrey Holle @ 2004-01-15 1:55 UTC (permalink / raw
To: linux-console
No joy.
The problem appears to be the '*' character.
If I substitute an actual cpp filename, the script works. However it
not very useful this way.
BobGian@U.Washington.edu wrote:
>>I'm having a problem with a simple script I'm trying to develop.
>>
>>Its name is fgrep and its text is:
>>
>> #!/bin/bash
>> find $1 -name $2 -exec grep -H $3 '{}' ';'
>>
>>I want to use it like "dgrep 'directory' 'file pattern' 'match text'.
>>
>>It doesn't match anything because of the $2 parameter. If I replace
>>"$2" with "*.cpp", it works.
>>
>>Can anybody tell me what is wrong and how to fix it?
>
>
> Try putting double-quotes around the parameters, like:
>
> find $1 -name "$2" -exec grep -H "$3" '{}' ';'
>
> This works for me in a similar script, at least in the csh shell.
>
> -Bob Giansiracusa
> -
> To unsubscribe from this list: send the line "unsubscribe linux-console" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
>
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: encapulating a find function in a script
@ 2004-01-16 19:53 BobGian
2004-01-16 20:09 ` Mike Castle
0 siblings, 1 reply; 7+ messages in thread
From: BobGian @ 2004-01-16 19:53 UTC (permalink / raw
To: dalgoda; +Cc: linux-console, jeff.holle
> You may also find that using xargs instead of -exec is much faster
> (fewer fork/execs).
>
> Something like:
>
> find "$1" -name "$2" -print0 | xargs -0 grep -H "$3"
>
> mrc
> --
Thanks! Tried it - works great. Runs two or three times as fast
and with half as many page faults.
-Bob
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: encapulating a find function in a script
2004-01-16 19:53 encapulating a find function in a script BobGian
@ 2004-01-16 20:09 ` Mike Castle
0 siblings, 0 replies; 7+ messages in thread
From: Mike Castle @ 2004-01-16 20:09 UTC (permalink / raw
To: BobGian; +Cc: dalgoda, linux-console, jeff.holle
On Fri, Jan 16, 2004 at 11:53:49AM -0800, BobGian@U.Washington.edu wrote:
> > You may also find that using xargs instead of -exec is much faster
> > (fewer fork/execs).
> >
> > Something like:
> >
> > find "$1" -name "$2" -print0 | xargs -0 grep -H "$3"
> >
> > mrc
> > --
>
> Thanks! Tried it - works great. Runs two or three times as fast
> and with half as many page faults.
Just remember that the -print0 and -0 are GNU extensions. If you work on
other OS's, you may need to look at going back to -exec, or see if they
offer any similar extensions.
mrc
--
Mike Castle dalgoda@ix.netcom.com www.netcom.com/~dalgoda/
We are all of us living in the shadow of Manhattan. -- Watchmen
fatal ("You are in a maze of twisty compiler features, all different"); -- gcc
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: encapulating a find function in a script
2004-01-15 1:55 ` Jeffrey Holle
@ 2004-01-16 21:09 ` Jeffrey Holle
0 siblings, 0 replies; 7+ messages in thread
From: Jeffrey Holle @ 2004-01-16 21:09 UTC (permalink / raw
To: linux-console
Got both original script and the suggested speed enhanced script to work
by putting a double double quote around the second parameter reference,
like ""$2"".
This was necessary to allow this parameter to be something like "*.cpp".
Without this treatment, *.cpp was expanded to the cpp files that
happened to be in my current directory.
I'm using Mandrake 9.1 linux and employing the bash command shell.
Do other linux/unix oses have this strange behavior?
Jeffrey Holle wrote:
> No joy.
> The problem appears to be the '*' character.
> If I substitute an actual cpp filename, the script works. However it
> not very useful this way.
>
> BobGian@U.Washington.edu wrote:
>
>>> I'm having a problem with a simple script I'm trying to develop.
>>>
>>> Its name is fgrep and its text is:
>>>
>>> #!/bin/bash
>>> find $1 -name $2 -exec grep -H $3 '{}' ';'
>>>
>>> I want to use it like "dgrep 'directory' 'file pattern' 'match text'.
>>>
>>> It doesn't match anything because of the $2 parameter. If I replace
>>> "$2" with "*.cpp", it works.
>>>
>>> Can anybody tell me what is wrong and how to fix it?
>>
>>
>>
>> Try putting double-quotes around the parameters, like:
>>
>> find $1 -name "$2" -exec grep -H "$3" '{}' ';'
>>
>> This works for me in a similar script, at least in the csh shell.
>>
>> -Bob Giansiracusa
>> -
>> To unsubscribe from this list: send the line "unsubscribe
>> linux-console" in
>> the body of a message to majordomo@vger.kernel.org
>> More majordomo info at http://vger.kernel.org/majordomo-info.html
>>
>
>
> -
> To unsubscribe from this list: send the line "unsubscribe linux-console" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
>
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2004-01-16 21:09 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-01-16 19:53 encapulating a find function in a script BobGian
2004-01-16 20:09 ` Mike Castle
-- strict thread matches above, loose matches on Subject: below --
2004-01-15 0:34 BobGian
2004-01-15 1:00 ` Mike Castle
2004-01-15 1:55 ` Jeffrey Holle
2004-01-16 21:09 ` Jeffrey Holle
2004-01-15 0:19 Jeffrey Holle
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).