Recursion in a bash script | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Recursion in a bash script

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 04-25-2013
mistsong1 mistsong1 is offline
Registered User
 
Join Date: Apr 2013
Last Activity: 25 April 2013, 4:57 PM EDT
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Recursion in a bash script

So the problem I am having is recursion with in bash. Specifically the program ideally takes either the current or provided directory, lists out everything in that directory, if it finds another directory the script should call itself and go into that directory. I managed to get it to dive into the next working directory with this code:

Code:
list=`ls`
for l in $list
 if [ -d $l ] ; then
  cd $l
  echo "I'm in $l"
  dir=`pwd`
  cd ../
  bash script.bash $dir
 fi
done

My thinking proccess behind this was that it dives into that directory, states it's in there, gets the full path of that directory and stores it in a variable, get out of that directory to were the bash script is, then passes the created variable into the script. I was hoping that the bash script would then dive into the specified directory and do the same thing but it doesn't sadly.
Sponsored Links
    #2  
Old 04-25-2013
Yoda's Avatar
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 22 October 2014, 8:12 PM EDT
Location: Galactic Empire
Posts: 3,387
Thanks: 235
Thanked 1,209 Times in 1,135 Posts
You didn't explain what issue that you are facing with your code!

But I noticed that you are missing a do

Code:
for l in $list
do
 if [ -d $l ] ; then

Sponsored Links
    #3  
Old 04-25-2013
mistsong1 mistsong1 is offline
Registered User
 
Join Date: Apr 2013
Last Activity: 25 April 2013, 4:57 PM EDT
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Yoda View Post
You didn't explain what issue that you are facing with your code!

But I noticed that you are missing a do

Code:
for l in $list
do
 if [ -d $l ] ; then

Sorry, the issue is that the code doesn't work as I want it to work. I don't quite understand how to recurse through the directories using a bash script, what the code I posted above does is it dives into the next directory, says it's in there, jumps out, and does it again for the next directory in the list. What it DOESN'T do is, go the extra mile like it's supposed to and recurse through the entire directory tree, like say it finds dir Test/, it dives inside, gets the full path, dives out, then calls itself with Test/ as its calling directory, then it's supposed to find all the dirs in there like Test1/, and repeat the process. I have a hard time explaining things sometimes so forgive me.
    #4  
Old 04-25-2013
Corona688 Corona688 is online now Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 23 October 2014, 1:48 PM EDT
Location: Saskatchewan
Posts: 19,665
Thanks: 819
Thanked 3,346 Times in 3,134 Posts
If you make that code a function, it can call itself.


Code:
recurse_directory() {
        local FILE
        for FILE in ./$1/*
        do
                [ -d "$FILE" ] && recurse_directory "./$1/$FILE"
        done
}

recurse_directory ./

Sponsored Links
    #5  
Old 04-25-2013
mistsong1 mistsong1 is offline
Registered User
 
Join Date: Apr 2013
Last Activity: 25 April 2013, 4:57 PM EDT
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Corona688 View Post
If you make that code a function, it can call itself.


Code:
recurse_directory() {
        local FILE
        for FILE in ./$1/*
        do
                [ -d "$FILE" ] && recurse_directory "./$1/$FILE"
        done
}

recurse_directory ./

Can you explain this code a little bit? I mainly don't get what the "local FILE" line does
Sponsored Links
    #6  
Old 04-25-2013
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
 
Join Date: May 2005
Last Activity: 23 October 2014, 11:38 AM EDT
Location: In the leftmost byte of /dev/kmem
Posts: 4,286
Thanks: 45
Thanked 824 Times in 651 Posts
Quote:
Originally Posted by mistsong1 View Post
Can you explain this code a little bit? I mainly don't get what the "local FILE" line does
It declares a variable named "FILE" which is local to the function - outside of this function it is not known.

I suggest you consult a man page for bash for keywords you don't know. Usually they are explained there in detail.

I hope this helps.

bakunin
Sponsored Links
    #7  
Old 04-25-2013
MadeInGermany MadeInGermany is offline Forum Advisor  
Registered User
 
Join Date: May 2012
Last Activity: 22 October 2014, 5:51 PM EDT
Location: Simplicity
Posts: 1,974
Thanks: 127
Thanked 578 Times in 524 Posts
Comment on the original code.
You don't make use of $1 parameter so don't need $dir
Indeed you can use the inheritance of the current work directory - but you need to change it before the recursive call.
So do not restore it i.e. do not cd .. (you may but not need to do cd .. afterwards)
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
script recursion superpointer Shell Programming and Scripting 6 02-12-2011 08:24 PM
Shell script to generate Fibonacci series using recursion Tapas Bose Shell Programming and Scripting 10 12-09-2009 01:05 PM
recursion script problem mac4rfree Shell Programming and Scripting 3 07-21-2009 07:15 AM
Help Help Help in recursion murtaza Shell Programming and Scripting 6 03-29-2007 10:26 AM
recursion gsjf Shell Programming and Scripting 1 08-26-2002 12:22 AM



All times are GMT -4. The time now is 01:51 PM.