zsh-workers
 help / color / mirror / code / Atom feed
* Recursion and shell functions
@ 2002-10-10 19:41 DervishD
  2002-10-10 19:44 ` DervishD
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: DervishD @ 2002-10-10 19:41 UTC (permalink / raw)
  To: Zsh

    Hi all :))

    First of all, please excuse this a-bit-off-topic question, but I
need to know if a feature available in zsh is portable.

    I need to do recursion in a directory tree and have two options;
first one is doing a 'for' loop recursively calling the shell
function which performs the actions:

    function () {

        for item
        do
            if [ -d $item ]
            then
                do_some_action
                chmod u+w $item
                cd $item
                function           <-- Overwrites 'item', of course
                chmod u-w $item    <-- Oh-oh... $item is not as before...
                ...
            fi 
        fi
        ...
    }

    Second is using 'find' to do the proper operations, although I
don't want the piece of software dependent on 'find'. The problem is
if I can do recursion safely (well, portably), if it is supported by
POSIX (I can't find a word about it in SuSv3) or at least if it is
common practice: don't want to find a tiny non-interactive shell
failing because of this...

    Just in case you can help me, I need to copy a hierarchy from one
place to another (where some files can be already present) setting
permissions in the process. Other solution I'm considering is doing a
'cp -fpR' over the tree and after that the recursion above for
setting the permissions. This may seems unreasonable (two
recursions...) but it's necessary since the recursive function call
overwrites local variables, and I *cannot* set them as local, since
'local' is not a portable keyword :((( I need 

    Suggestions welcome and thanks in advance :))

    Raúl


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Recursion and shell functions
  2002-10-10 19:41 Recursion and shell functions DervishD
@ 2002-10-10 19:44 ` DervishD
  2002-10-10 20:02 ` Jason Price
  2002-10-11  9:02 ` Oliver Kiddle
  2 siblings, 0 replies; 7+ messages in thread
From: DervishD @ 2002-10-10 19:44 UTC (permalink / raw)
  To: Zsh

    Hi all :)))

>         for item

    Sorry, this is 'for item in *'...

    Raúl


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Recursion and shell functions
  2002-10-10 19:41 Recursion and shell functions DervishD
  2002-10-10 19:44 ` DervishD
@ 2002-10-10 20:02 ` Jason Price
  2002-10-10 21:15   ` DervishD
  2002-10-11  9:02 ` Oliver Kiddle
  2 siblings, 1 reply; 7+ messages in thread
From: Jason Price @ 2002-10-10 20:02 UTC (permalink / raw)
  To: DervishD; +Cc: Zsh

DervishD <raul@pleyades.net> writes:

>     First of all, please excuse this a-bit-off-topic question, but I
> need to know if a feature available in zsh is portable.

You could re-invent the wheel, but why?

(cd <your source dir> ; tar -cpvf - .) | ( cd <your dest dir> ; tar -xpf -)

Or if you want to preserve things, I imagine a similar incantation with
rsync will work just as well.

The above works amazingly well with ssh too:

cd <your source> ; tar -cpvf - . | ssh -l <name> <host> "cd dir ; tar -xpf -"

Rsync is good, but in some comparisons tar over rsh worked better for
large directories with lots of files in the 'copy everything first time'
case.  In the 'just the delta's needed' case, rsync wins.

Jason


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Recursion and shell functions
  2002-10-10 20:02 ` Jason Price
@ 2002-10-10 21:15   ` DervishD
  0 siblings, 0 replies; 7+ messages in thread
From: DervishD @ 2002-10-10 21:15 UTC (permalink / raw)
  To: Jason Price; +Cc: Zsh

    Hi Jason :)

> >     First of all, please excuse this a-bit-off-topic question, but I
> > need to know if a feature available in zsh is portable.
> You could re-invent the wheel, but why?
> (cd <your source dir> ; tar -cpvf - .) | ( cd <your dest dir> ; tar -xpf -)

    Thanks for your answer, Jason, but I don't want to make the
software dependent on 'tar'. In fact I don't have tar on my system! I
use 'pax' instead ;))

    The aim is to depend only on the shell and POSIX utilities (like
'cp', 'rm, etc... you know.). I want to impose little or no
dependencies. 'tar' is not a good solution for me, I prefer 'pax',
although is not as extended by now, 'rsync' seems to big for copying
a few bunch of files, and 'ssh'... well...

    Thanks again.

    Raúl


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Recursion and shell functions
  2002-10-10 19:41 Recursion and shell functions DervishD
  2002-10-10 19:44 ` DervishD
  2002-10-10 20:02 ` Jason Price
@ 2002-10-11  9:02 ` Oliver Kiddle
  2002-10-11 11:05   ` DervishD
  2 siblings, 1 reply; 7+ messages in thread
From: Oliver Kiddle @ 2002-10-11  9:02 UTC (permalink / raw)
  To: DervishD; +Cc: zsh-workers

On 10 Oct, you wrote:

>     First of all, please excuse this a-bit-off-topic question, but I
> need to know if a feature available in zsh is portable.
> 
>     I need to do recursion in a directory tree and have two options;
> first one is doing a 'for' loop recursively calling the shell
> function which performs the actions:


> setting the permissions. This may seems unreasonable (two
> recursions...) but it's necessary since the recursive function call
> overwrites local variables, and I *cannot* set them as local, since
> 'local' is not a portable keyword :((( I need 

If you know that you will at least have ksh88, you can get local
variables by using the function name { ... } syntax for functions and
using typeset to declare variables.

The bourne shell doesn't have local variables except for the
positional parameters and you can use them. I think this will be
portable:

func() {
  set *
  while [ "$1" ]; do
    if [ -d "$1" ]; then
      cd $1
      func
      cd ..
    fi
    shift
  done
}

Something like find is going to be more reliable though.

Oliver

This e-mail and any attachment is for authorised use by the intended recipient(s) only.  It may contain proprietary material, confidential information and/or be subject to legal privilege.  It should not be copied, disclosed to, retained or used by, any other party.  If you are not an intended recipient then please promptly delete this e-mail and any attachment and all copies and inform the sender.  Thank you.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Recursion and shell functions
  2002-10-11  9:02 ` Oliver Kiddle
@ 2002-10-11 11:05   ` DervishD
       [not found]     ` <E1802KE-0001g0-00@bimbo.logica.co.uk>
  0 siblings, 1 reply; 7+ messages in thread
From: DervishD @ 2002-10-11 11:05 UTC (permalink / raw)
  To: Oliver Kiddle; +Cc: zsh-workers

    Hi Oliver :)

    Thanks a lot for your answer :)

> If you know that you will at least have ksh88, you can get local
> variables by using the function name { ... } syntax for functions
> and using typeset to declare variables.

    The only requirement is a SuSv3 compatible shell :(

> The bourne shell doesn't have local variables except for the
> positional parameters and you can use them.

    Of course, how I missed?!!!

> I think this will be portable:
> 
> func() {
>   set *

    I don't catch this :??? What is it for? Oh, I know, it sets '$1',
'$2', etc... to the files and directories in current dir :)) Very
inteligent :))

> Something like find is going to be more reliable though.

    Of course. I'm considering adding the dependency on 'find', but I
will really try to avoid :)) Thanks for your example. You're great,
Oliver :)

    Raúl


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Recursion and shell functions
       [not found]     ` <E1802KE-0001g0-00@bimbo.logica.co.uk>
@ 2002-10-11 18:27       ` DervishD
  0 siblings, 0 replies; 7+ messages in thread
From: DervishD @ 2002-10-11 18:27 UTC (permalink / raw)
  To: Oliver Kiddle; +Cc: Zsh

    Hi Oliver :))

> >     The only requirement is a SuSv3 compatible shell :(
> I'm not sure whether SuSv3 defines a behaviour for a recursive
> function. If you test it on a few implementations it should be okay.

    It does not, but it is supported at least in zsh and bash. When
released I suppose that people will test it under more shells.

> > >   set *
> Note than in a zsh that isn't emulating Bourne, this might result
> in an error when * doesn't match anything.

    I've thought of a solution: we create some 'fake' file so the
asterisk at least expands to that file. There is no problem with
this, since we have control over the source directory and we can
write over it. Anyway we would like a better solution O:)))

    All this is for a 'configure' clone that I'm doing called MOBS
(My Own Build System) that I use in my projects. It's GPL'd and will
be released (at least!) in a couple of weeks.

> > > Something like find is going to be more reliable though.
> If you do this, you might want to take a little care over a few things

    Thanks for your advise :))). I'll try to make it as reliable as I
can. And if I think of a better solution for the '*' expansion issue,
I will tell on the list :)

    Thanks a lot for your help :)))) You've saved me a lot of (unpaid
but pleasant) work :)))

    Raúl


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2002-10-11 18:31 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-10-10 19:41 Recursion and shell functions DervishD
2002-10-10 19:44 ` DervishD
2002-10-10 20:02 ` Jason Price
2002-10-10 21:15   ` DervishD
2002-10-11  9:02 ` Oliver Kiddle
2002-10-11 11:05   ` DervishD
     [not found]     ` <E1802KE-0001g0-00@bimbo.logica.co.uk>
2002-10-11 18:27       ` DervishD

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

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 NNTP newsgroup(s).