LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-16-2006, 03:59 PM   #1
Melsync
Member
 
Registered: Sep 2005
Posts: 75

Rep: Reputation: 15
Smile 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
 
Old 01-16-2006, 06:22 PM   #2
Melsync
Member
 
Registered: Sep 2005
Posts: 75

Original Poster
Rep: Reputation: 15
dirs=`ls -d */`
cd $1
hereiam
cd $2
hereiam

seems to work but it needs some iteration and test construct...
 
Old 01-16-2006, 09:41 PM   #3
primo
Member
 
Registered: Jun 2005
Posts: 542

Rep: Reputation: 34
Try with:
Code:
#!/bin/sh
hereiam ()
{
echo "Here I am now:" $(pwd)
}

find . -type d | while read dir ; do ( cd "$dir" ; hereiam ) ; done
 
Old 01-17-2006, 06:13 AM   #4
Melsync
Member
 
Registered: Sep 2005
Posts: 75

Original Poster
Rep: Reputation: 15
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!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
gcc: recursive function quirk Disruptor Linux - Software 4 09-23-2014 05:45 AM
using return in recursive function hubabuba Programming 9 10-10-2005 09:59 AM
error recursive function in c shams Programming 3 07-28-2004 08:44 PM
Timed execution of a function arvind_tyche Programming 1 05-07-2004 08:25 AM
Recursive Function TriggerJ Programming 4 02-02-2004 08:58 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:22 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration