From: laredotornado on
Hi,

I'm using Mac 10.6.3 with a bash shell. Currently I use this command
to search for text in a bunch of files ...

find . -name "*.xml" -exec grep -i 'fillInBlankTestItem' /dev/null {}
+

I was wondering if someone knows how I can define a shorter command
"myfind", that I could write like such

myfind . -name "*.xml" "fillInBlankTestItem"

tha would map the arguments to the above. It could be something I
defined in my ~/.bash_profile script. Any advice is appreciated.
Thanks, - Dave
From: Stachu 'Dozzie' K. on
On 2010-04-27, laredotornado <laredotornado(a)zipmail.com> wrote:
> Hi,
>
> I'm using Mac 10.6.3 with a bash shell. Currently I use this command
> to search for text in a bunch of files ...
>
> find . -name "*.xml" -exec grep -i 'fillInBlankTestItem' /dev/null {}
> +
>
> I was wondering if someone knows how I can define a shorter command
> "myfind", that I could write like such
>
> myfind . -name "*.xml" "fillInBlankTestItem"
>
> tha would map the arguments to the above. It could be something I
> defined in my ~/.bash_profile script. Any advice is appreciated.

Aliases can't do that. Use shell functions instead.

--
Secunia non olet.
Stanislaw Klekot
From: laredotornado on
On Apr 27, 8:00 am, "Stachu 'Dozzie' K."
<doz...(a)go.eat.some.screws.spammer.invalid> wrote:
> On 2010-04-27, laredotornado <laredotorn...(a)zipmail.com> wrote:
>
> > Hi,
>
> > I'm using Mac 10.6.3 with a bash shell.  Currently I use this command
> > to search for text in a bunch of files ...
>
> > find . -name "*.xml" -exec grep -i 'fillInBlankTestItem' /dev/null {}
> > +
>
> > I was wondering if someone knows how I can define a shorter command
> > "myfind", that I could write like such
>
> > myfind . -name "*.xml" "fillInBlankTestItem"
>
> > tha would map the arguments to the above.  It could be something I
> > defined in my ~/.bash_profile script.  Any advice is appreciated.
>
> Aliases can't do that. Use shell functions instead.
>
> --
> Secunia non olet.
> Stanislaw Klekot

Ok. How would I do what I want to do with this so called "shell
function" of which you speak? - Dave
From: Bit Twister on
On Tue, 27 Apr 2010 06:28:58 -0700 (PDT), laredotornado wrote:

> Ok. How would I do what I want to do with this so called "shell
> function" of which you speak? - Dave

put your code inside a function declaration, set it up to use any
command line arguments.

Suggest you create a script with your new function, test script,
then put function into your .bashrc file, launch an xterm
and verify if .bashrc works.

Some light reading found here http://tldp.org/LDP/abs/html/index.html
and search for function.

From: Icarus Sparry on
On Tue, 27 Apr 2010 06:28:58 -0700, laredotornado wrote:

> On Apr 27, 8:00 am, "Stachu 'Dozzie' K."
> <doz...(a)go.eat.some.screws.spammer.invalid> wrote:
>> On 2010-04-27, laredotornado <laredotorn...(a)zipmail.com> wrote:
>>
>> > Hi,
>>
>> > I'm using Mac 10.6.3 with a bash shell.  Currently I use this command
>> > to search for text in a bunch of files ...
>>
>> > find . -name "*.xml" -exec grep -i 'fillInBlankTestItem' /dev/null {}
>> > +
>>
>> > I was wondering if someone knows how I can define a shorter command
>> > "myfind", that I could write like such
>>
>> > myfind . -name "*.xml" "fillInBlankTestItem"
>>
>> > tha would map the arguments to the above.  It could be something I
>> > defined in my ~/.bash_profile script.  Any advice is appreciated.
>>
>> Aliases can't do that. Use shell functions instead.
>>
>> --
>> Secunia non olet.
>> Stanislaw Klekot
>
> Ok. How would I do what I want to do with this so called "shell
> function" of which you speak? - Dave

If it is allowed to put the pattern for grep as the first argument then
things become simpler.

myfind(){
pattern=$1
shift
find "$@" -exec grep -i "$pattern" /dev/null {}
}

But for something like this you would be better off creating it as a
shell script. The advantage of having it as a function is that it is
slightly faster to invoke it. The downsides are that it slows every
command down a tiny amount and that the command can not be directly used
by other scripts or by users of other shells. Since this command will
take a significant amount to time to run (it needs to walk part of the
file-system and run a command on any file that matches) the small amount
of time saved on invoking it will be insignificant in the big picture.

So I would suggest you write a file, e.g. ~/bin/myfind, containing

#!/bin/sh
pattern=$1
shift
find "$@" -exec grep -i "$pattern" /dev/null {}

make the file executable, using the chmod command, make sure that your
PATH variable contains the directory that includes the file, and then
just use it.