It is currently Tue, 30 Nov 2021 17:03:12 GMT



 
Author Message
 sh: how to write recursive functions in Bourne shell?
Our friend, engp3...@leonis.nus.sg (Yeo Eng Hee), wrote:

        Sure:

                for file in <blah>
                .....

                        do_it()

                and file gets clobbered in the do_it because sh has no notion of
                local variables or scope...

Soren



 Mon, 23 Mar 1998 03:00:00 GMT   
 sh: how to write recursive functions in Bourne shell?
Yeo Eng Hee <engp3...@leonis.nus.sg> wrote:

This is wrong.  The correct syntax to call the function is

        do_it

You also don't call the function in the script -- you only define it.
You should put a "do_it" line at the end.

-zefram



 Mon, 23 Mar 1998 03:00:00 GMT   
 sh: how to write recursive functions in Bourne shell?
Hi,

 I have been trying to write a script in shell with a function that calls
itself recursively.  This is the first time I am attempting to write a
function inside a shell script.  What I wanted to do is to find all the
compressed files in the subdirectories and uncompress them.  Can anyone
see what's wrong with this script I wrote?

Thanks in advance.

Eng Hee

#! /bin/sh
#

do_it() {
for item in `ls`
do
  if [ -f $item ]
  then
    uncompress $item
  fi
  if [ -d $item ]
  then
    cd $item
    do_it()
    cd ..
  fi
done



 Mon, 23 Mar 1998 03:00:00 GMT   
 sh: how to write recursive functions in Bourne shell?
In article <44vsmv$...@nuscc.nus.sg>,
Yeo Eng Hee <engp3...@leonis.nus.sg> wrote:
: Hi,
:
:  I have been trying to write a script in shell with a function that calls
: itself recursively.  

Ah, a first-year computer science student.

: This is the first time I am attempting to write a function inside
: a shell script.  What I wanted to do is to find all the compressed
: files in the subdirectories and uncompress them.  Can anyone see
: what's wrong with this script I wrote?

It's far too complicated.  Try

#!/bin/sh
find . -name '*.Z' -print | xargs uncompress

Or, if you're using zsh, just type

uncompress **/*.Z

--Dave
--
http://armf18.dow.on.doe.ca:6700/~dbrown/ Dave Brown, goo, rogue news admin

"Please stop posting the same post in the linked effect all over the internet."
                               --some AOL user on crossposting



 Mon, 23 Mar 1998 03:00:00 GMT   
 sh: how to write recursive functions in Bourne shell?

--
Dana French                                      E-Mail: dfre...@aig.com
Applied Intelligence Group                        Phone: (405) 341-7700
501 East 15th Street, Suite 202                     Fax: (405) 341-8898
Edmond, OK  73013



 Tue, 24 Mar 1998 03:00:00 GMT   
 sh: how to write recursive functions in Bourne shell?
In my last post I misread your question and posted a reply which showed
an example of how to "compress" rather than "uncompress".  the correct
method should be.

find /top_dir -name \*.Z -exec uncompress {} \;

where /top_dir represents the top level directory where you want the
uncompressing to begin.

--
Dana French                                      E-Mail: dfre...@aig.com
Applied Intelligence Group                        Phone: (405) 341-7700
501 East 15th Street, Suite 202                     Fax: (405) 341-8898
Edmond, OK  73013



 Tue, 24 Mar 1998 03:00:00 GMT   
 
   [ 6 post ] 

Similar Threads

1. Recursive Bourne shell script problem with metacharacters and spaces

2. Writing portable C, bourne sh, PRO*C.

3. Bourne Shell (/bin/sh) counting?

4. Bourne Shell (/bin/sh) counting?

5. of ~/.profile, bourne shell, and ksh93's ${.sh.version}

6. Bourne shell (sh) behaviour

7. Bourne Shell (/bin/sh) & clobber

8. Light Speed Bourne Shell! (was: Bourne shell tricks)


 
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software