From: pk on
PGK wrote:

>> So you can do something like
>>
>> find ... -exec sh -c 'do your stuff with "$1" here' sh {} \;
>>
>> quotes are important.
>
> Ok, thanks. I tried:
> find ./*/ -maxdepth 0 -type d -print0 -exec sh -c myecho sh {} \;
> but I get the error
> sh: myecho: command not found

In this context, -print0 is totally meaningless.

First, you must export the function, so

export -f myecho

and then, as I said, QUOTES ARE IMPORTANT.

find ... -exec sh -c 'myecho "$1"' sh {} \;

and anyway, everything in your command hints to the fact that there's
probably a simpler way to do whatever you're trying to do (which you haven't
explained).
From: PGK on
On 2 Mar, 14:04, pk <p...(a)pk.invalid> wrote:
> PGK wrote:
> >> So you can do something like
>
> >> find ... -exec sh -c 'do your stuff with "$1" here' sh {} \;
>
> >> quotes are important.
>
> > Ok, thanks. I tried:
> > find ./*/ -maxdepth 0 -type d -print0 -exec sh -c myecho sh {} \;
> > but I get the error
> > sh: myecho: command not found
>
> In this context, -print0 is totally meaningless.
>
> First, you must export the function, so
>
> export -f myecho
>
> and then, as I said, QUOTES ARE IMPORTANT.
>
> find ... -exec sh -c 'myecho "$1"' sh {} \;
>
> and anyway, everything in your command hints to the fact that there's
> probably a simpler way to do whatever you're trying to do (which you haven't
> explained).

Thankyou. I had already tried a lot of "QUOTES ARE IMPORTANT" ideas,
but it was the "export" step you mentioned which was crucial (and I
still don't understand the final sh {}). Anyway, the following program
does what I was looking for:

function doit {
cd "$1"
ls
cd -
}

export -f doit
find ./*/ -maxdepth 0 -type d -exec sh -c 'doit "$1"' sh {} \;
From: Ben Bacarisse on
PGK <graham.keir(a)gmail.com> writes:

> On 2 Mar, 13:25, pk <p...(a)pk.invalid> wrote:
>> PGK wrote:
>>
>> > I'm trying to write a bash script which will cd into each directory in
>> > the current one; do something; then cd back out again. The problem I
>> > have is that some of the client's directories contain spaces.
>>
>> > My best effort is below.
>>
>> > function myecho {
>> >   echo $1
>> > }
>>
>> > find ./*/ -maxdepth 0 -type d -print0 -exec   echo {} \;
>> > find ./*/ -maxdepth 0 -type d -print0 -exec myecho {} \;
>>
>> > With "echo" everything runs fine, but "myecho" gives multiple "No such
>> > file or directory" errors. Can anyone give some advice?
>>
>> > Eventually I would replace myecho with something like doit:
>>
>> > function doit {
>> >   cd $1/src
>> >   make
>> >   cd -
>> > }
>>
>> You can't directly call a function in a -exec action, becuase it can't be
>> exec()ed.
>>
>> So you can do something like
>>
>> find ... -exec sh -c 'do your stuff with "$1" here' sh {} \;
>>
>> quotes are important.
>
> Ok, thanks. I tried:
> find ./*/ -maxdepth 0 -type d -print0 -exec sh -c myecho sh {} \;
> but I get the error
> sh: myecho: command not found

The shell that 'find' runs can't see your functions (they are in the
script that runs 'find' not in the shell command that 'find'
executes).

You could put the "doit" processing into a file and run that, or you
could put it directly in the -c argument (which is what pk was
suggesting, I think), but I think you should check to see if your
'find' supports -execdir since that could be exactly what you want.

--
Ben.
From: pk on
PGK wrote:

> but it was the "export" step you mentioned which was crucial (and I
> still don't understand the final sh {}).

It's in the manual:

-c string
If the -c option is present, then commands are read from string. If there
are arguments after the string, they are assigned to the positional
parameters, starting with $0.

So "sh" is $0 (in this case, you could really use anything, "sh" looks like
the most sensible option to me), and {} (which find replaces with the actual
name as a single argument) is $1.

From: Seebs on
On 2010-03-02, PGK <graham.keir(a)gmail.com> wrote:
> function myecho {
> echo $1
> }
>
> find ./*/ -maxdepth 0 -type d -print0 -exec echo {} \;
> find ./*/ -maxdepth 0 -type d -print0 -exec myecho {} \;

This won't work, because "find" is going to *execute* another program.

> function doit {
> cd $1/src
> make
> cd -
> }

Possibly "make -C {}"? I don't remember off the top of my head how portable
that is. Also, if you're using -exec, you don't need -print0. I also
don't understand why you're searching ./*/ with -maxdepth 0; wouldn't it be
easier to do . with -maxdepth 1?

Note that you don't need the "cd -", because each command is run in its own
environment, so there's no need to move it back to another directory.

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / usenet-nospam(a)seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!