From: PGK on
Hi everyone,

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 -
}
From: pk on
PGK wrote:

> Hi everyone,
>
> 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.
From: Ed Morton on
On 3/2/2010 7:15 AM, PGK wrote:
> Hi everyone,
>
> 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 -
> }

First quote your variables (e.g. so $1 becomes "$1") then get back to us if you
still have a problem. ALWAYS quote your variables unless you have a specific
reason not to.

Ed.
From: PGK on
On 2 Mar, 13:25, pk <p...(a)pk.invalid> wrote:
> PGK wrote:
> > Hi everyone,
>
> > 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
From: PGK on
On 2 Mar, 13:25, Ed Morton <mortons...(a)gmail.com> wrote:
> On 3/2/2010 7:15 AM, PGK wrote:
>
>
>
> > Hi everyone,
>
> > 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 -
> > }
>
> First quote your variables (e.g. so $1 becomes "$1") then get back to us if you
> still have a problem. ALWAYS quote your variables unless you have a specific
> reason not to.
>
>      Ed.

I have tried it with "$1" but I get the same error messages. I will
leave it as "$1" for now, thanks.