LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   recursive execution of function in subdirectories (http://www.linuxquestions.org/questions/programming-9/recursive-execution-of-function-in-subdirectories-404167/)

Melsync 01-16-2006 04:59 PM

recursive execution of function in subdirectories
 
I have this type of bash script to execute a function recursively in the subdirectories (the real one is more useful than the one in the example below :) ):
Code:

#!/bin/sh
hereiam ()
{
echo "Here I am now:" $(pwd)
}

find . -type d | while read dir; do hereiam; done

I am finding some problems with the function but I don't know if it is due to the function itself or by the way that find | while ( or find | xargs for that sake) operate.

How can I execute the function recursively without 'find'?
Thanks!

Osio

Melsync 01-16-2006 07:22 PM

dirs=`ls -d */`
cd $1
hereiam
cd $2
hereiam

seems to work but it needs some iteration and test construct...

primo 01-16-2006 10:41 PM

Try with:
Code:

#!/bin/sh
hereiam ()
{
echo "Here I am now:" $(pwd)
}

find . -type d | while read dir ; do ( cd "$dir" ; hereiam ) ; done


Melsync 01-17-2006 07:13 AM

command in parenthesis
 
Quote:

Originally Posted by primo
Try with:
Code:

( cd "$dir" ; hereiam )

Yes, my function in real life does need to be at the working directory, so
Code:

cd "$dir"
is what I needed.
Thank you, primo! :)


All times are GMT -5. The time now is 11:23 AM.