Problem with Recursive function


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with Recursive function
# 1  
Old 02-03-2007
Problem with Recursive function

Hi all,

I have to move all the files in a tree directory structure to a single directory. Inorder to know which file is from which directory , i'll have to add the name of the directory to the file name. For this i wrote a recursive function which is as follows
---------------------------------------------
datamover()
{
cd $1
#OLD_DIR=$2
for i in *
do
if [ -d $i ]
then
TMP_DIR=$i
datamover $TMP_DIR
echo $i
else
mv $i ../$1_$i

if [ -f $1/ ]
then
echo some files still exist
else
cd ..
rmdir $1
fi

fi
done
}
----------------------------------------------------------------
When ever I execute the same on a directory structure as
DirA --> DirB --> DirL -->fileN
--> DirM-->fileO
--->DirC --> DirP-->fileR
--> DirQ-->fileS

the program is giving the output as follows
DirA -->DirB -->DirL_fileN
-->DirM_fileO
--->DirC --> DirP-->fileR
--> DirQ-->fileS

and the debug ouptut is as follows
------------------------------------------------------
+ echo Dir Name is DirA
Dir Name is DirA
+ MAIN_DIR=DirA
+ datamover DirA
+ cd DirA
+ '[' -d DirB ']'
+ TMP_DIR=DirB
+ datamover DirB
+ cd DirB
+ '[' -d DirL ']'
+ TMP_DIR=DirL
+ datamover DirL
+ cd DirL
+ '[' -d fileN ']'
+ mv fileN ../DirL_fileN
+ '[' -f DirL/ ']'
+ cd ..
+ rmdir DirL
+ echo fileN
fileN
+ '[' -d DirM ']'
+ TMP_DIR=DirM
+ datamover DirM
+ cd DirM
+ '[' -d fileO ']'
+ mv fileO ../DirM_fileO
+ '[' -f DirM/ ']'
+ cd ..
+ rmdir DirM
+ echo fileO
fileO
+ echo fileO
fileO
+ '[' -d DirC ']'
+ mv DirC ../DirA_DirC
mv: cannot stat `DirC': No such file or directory
+ '[' -f DirA/ ']'
+ cd ..
+ rmdir DirA
rmdir: `DirA': No such file or directory
--------------------------------------------------------------

Can anybody tell me wht's the problem with the code and how to correct it?
the output i require in this scenario will be as
DirA-->DirB_DirL_fileN
-->DirB_DirM_fileO
-->DirC_DirP_fileR
-->DirC_DirQ_fileS

Thanks in Advance
# 2  
Old 02-03-2007
Here's a simple starting point....
Code:
$ find dirA -type f -print | while read filename; do
>    mv ${filename} ./destDir/`echo ${filename} | sed 's!/!_!g'`
> done
$ find dirA -type f -print
$ ls destDir
dirA_dirB_dirE_file_4  dirA_dirC_dirF_dirI_file_7  dirA_dirC_file_2       dirA_dirD_file_3
dirA_dirB_file_1       dirA_dirC_dirF_file_5       dirA_dirD_dirG_file_6

As you can see, all files are moved from dirA to destDir.

You can then just rm -rf dirA

Cheers
ZB
# 3  
Old 02-03-2007
Quote:
Originally Posted by zazzybob
Here's a simple starting point....
Code:
$ find dirA -type f -print | while read filename; do
>    mv ${filename} ./destDir/`echo ${filename} | sed 's!/!_!g'`
> done
$ find dirA -type f -print
$ ls destDir
dirA_dirB_dirE_file_4  dirA_dirC_dirF_dirI_file_7  dirA_dirC_file_2       dirA_dirD_file_3
dirA_dirB_file_1       dirA_dirC_dirF_file_5       dirA_dirD_dirG_file_6

As you can see, all files are moved from dirA to destDir.

You can then just rm -rf dirA


Cheers
ZB
This is the way to do it.

The original script fails because of the
for i in *
statement

The * is evaluated at start of the function and doesn't anticipate on files moved to that directory from a subdirectory.
# 4  
Old 02-03-2007
CPU & Memory

zazzybob's solution is an elegant and simple solution.

In response to recursion within a Korn shell function, there are two points to consider. All variables, including the current working directory, are global unless specifically defined with the function which then makes them local within the function.
Code:
typeset FILENAME=/etc/hosts

Function ()
{
    FILENAME=/etc/resolv.conf
    echo $FILENAME
}

echo $FILENAME
Function
echo $FILENAME

would produce

/etc/hosts
/etc/resolv.conf
/etc/resolv.conf

However,

Code:
typeset FILENAME=/etc/hosts

Function ()
{
    typeset FILENAME

    FILENAME=/etc/resolv.conf
    echo $FILENAME
}

echo $FILENAME
Function
echo $FILENAME

would produce
/etc/hosts
/etc/resolv.conf
/etc/hosts

A quick and dirty way to make variables local to the function INCLUDING the working directory is to execute the function in its own shell by placing parenthesis within the function brackets. The function will get its own copy of the environment when called and any changes, including the directory, to that environment will not be reflected in the calling script which is its own function.

Code:
typeset FILENAME=/etc/hosts

Function ()
{ (
    FILENAME=/resolv.conf
    echo $FILENAME
) }

echo $FILENAME
Function
echo $FILENAME

would produce
/etc/hosts
/etc/resolv.conf
/etc/hosts

Last edited by hegemaro; 02-03-2007 at 10:41 AM..
# 5  
Old 02-03-2007
Thanks zazzybob. The solution was fantastic.
Smilie
Thanks to everyone who has given some more hints about the solution.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

recursive function

Hello forum members, Please wirte a sample program for print the 1 - 100 and 100 -1 using recursive function. Thanks & regards Siva Rangnanath (2 Replies)
Discussion started by: workforsiva
2 Replies

2. Shell Programming and Scripting

Run recursive function with variables over SSH

Hi, I don't know if you can help or if this is even possible, but I am trying to run the following function over an ssh and (depending on the itteration I choose) keep getting unexpected token or undefined symbol errors. The function is: killtree() { typeset parent=$1 typeset child... (1 Reply)
Discussion started by: RECrerar
1 Replies

3. Shell Programming and Scripting

perl recursive function issue

I am facing some problem in perl recurssive function function my @array_parent = (Some inegers); my $outputfile = 'output.txt'; my $master_file = 'master.txt'; open (MASTER,"$>>master.txt"); foreach my $child (@array_parent){ my $line = `grep "$child" "$outputfile"`; ... (1 Reply)
Discussion started by: pritish.sas
1 Replies

4. Shell Programming and Scripting

AWK Problem in recursive function

Hi, I have a file like this SPF_HC00001|iCalcular_Monto_Minimo|--->|SPF_HC00028|pstcObtener_Monto_Minimo SPF_HC00004|iCalcular_Incrementos|--->|SPF_HC00032|pstcObtener_Num_Incrementos SPF_HC00005|iCalcular_Articulo_167_Reformado|--->|SPF_HC00031|pstcObtener_Por_CB_Inc... (2 Replies)
Discussion started by: kcoder24
2 Replies

5. Shell Programming and Scripting

Recursive function and arrays

I have the following function in a bash script that fails to return the sorted array. I think the problem lies in the recursion not correctly passing the arrays, but I can't tell what I'm doing wrong. Anyone see the problem? function quicksort () { local array=( `echo "$1"` ) local... (7 Replies)
Discussion started by: tkg
7 Replies

6. Shell Programming and Scripting

Not able to store the results of perl recursive function when applied under for loop

Hi Perl Gurus , need URGENT HELP PLEASE !!!!! I have one recursive Perl function which takes path of any directory as argument and returns array containing all the sub folders inside it recursively. Now the problem is that it works well if i use it with one time but the problem is that when... (0 Replies)
Discussion started by: anthriksh2000
0 Replies

7. Shell Programming and Scripting

Recursive function in unix

Can someone tell me, how do i write a recursive code using shell ( eg like 'for' loop in C) which outputs the record to a database table as one row per iteration? (7 Replies)
Discussion started by: goutam_igate
7 Replies

8. Shell Programming and Scripting

Recursive function call problem

This is shell script I have made to lists out directory contents and filenames for any given directory (without using ls command). There is some problem in dirfunc function call which I have marked 1 is not working. Can anybody suggest what is the problem there and how should I correct it. ... (2 Replies)
Discussion started by: netresearch
2 Replies

9. Shell Programming and Scripting

Script problem due to recursive directories Help please

Hello everyone , I am looking for a better solution then the one I have created for the my Task The task is: Create an automated script that will check for Uploads in a specified Directory and move them to another specified Directory if the files are completely uploaded. Files are FTP'd to... (2 Replies)
Discussion started by: robertmcol
2 Replies

10. Programming

recursive function

Hi everyone, i need your input on this. We can express a function recursivly like this A(n) = (2 n = 0 5 n = 1 A(n − 1) + A(n − 2) % 47 n > 1 How would i go about constructing a recursive function for this?... (1 Reply)
Discussion started by: bebop1111116
1 Replies
Login or Register to Ask a Question