Recursion to Copy a Directory structure

I have the below code to recurse through a given file path. I am able to go to the leaf level once recursively but traversing out is not possible as the required varaibles don't have the values on return. Both typeset and local don't work for my variable definitions. I have the o/p of the sample run attached as well with some debug statements. Pls. help me get out of this. Thanks a ton in advance.

===============
CODE

CreateElement()
{
srcDir=$1
destDir=$2
lastKnownDir="$3"

    cd $srcDir
    depth=\`ls | cut -f1 | wc -l\`

echo "Inside CreateElement with Depth -> $depth \n"

    j=1
    while [ $j -le $depth ]
    do
            a=\`ls | cut -f1 | head -$j | tail -1\`

echo "0-> $a is the file/dir being processed and $j -> is j value\n"
isdir=`ls -p | cut -f1 | head -$j | tail -1 | grep /`
#echo "1-> isdir = $isdir ,\n"
if [ "$isdir" != "" ]; then
cd $destDir
if [ -d $a ]; then
echo "\nDirecotry $a already exists!\n"
else
#cc_mkdir $a
mkdir "$a"
fi
srcDir=$srcDir/$isdir
destDir=$destDir/$isdir
lastKnownDir="$isdir"
echo "1 -> lastKnownDir = $lastKnownDir\n"
CreateElement $srcDir $destDir $lastKnownDir
else
cd $destDir
echo "2 -> destDir is $destDir in the file handling code\n"
echo "3 -> srcDir is $srcDir in the file handling code\n"
echo "4 -> lastKnownDir is $lastKnownDir in FHC\n"
if [ -f $a ]; then
echo "\nFile $a already exists!\n"
else
#cc_mkelem $a
echo "create element -> $a \n"
fi
cp $srcDir$a .
fi
j=`expr $j + 1`
echo "5 -> J value at end of loop is $j, before Done\n"
done

    if [ $j -gt $depth ]; then

echo "$j == $depth moving one directory up !!!!!\n"
echo "lastknownDir at end is -> $lastKnownDir *****\n"
srcDir=`echo $srcDir | sed -e "s/"$lastKnownDir"//"`
destDir=`echo $destDir | sed -e "s/$lastKnownDir//"`
fi

echo "isdir is -> $isdir \n"
echo "\nsrcDir -> $srcDir and destDir -> $destDir \n"

echo "Just before return from CreateElement\n"
return
}

#DESTN="$IPCBU_AUTO_HOME/scripts/"
DESTN="/users/cpati/"

clear

### Check for number of arguments ###
if [ $# -ne 1 ]; then
echo "Usage : $0 <Absolute Path of source directory>\n"
exit
fi

SRC=$1

### Check whether Source path has a / at the end ###
slash=`echo $1 | grep /$`
if [ "$slash" = "" ]; then
SRC=$SRC"/"
fi

### First level ###
cd $SRC
count=`ls | cut -f1 | wc -l`

## Check for existance of files and dir ##
if [ $count -eq 0 ]; then
echo "WARNING!!There are no files in the specified source to check in\n"
exit
fi

## Check for change in destn directory ##
echo "\n"
echo "The default directory to check in your feature code is $DESTN"
echo "Do you wish to change the destination(Y/N)?\c"
read option
if [ "$option" = "Y" ]; then
echo "Please specify the absolute path of the new destination."
read DESTN
fi

### Check whether Destination path has a / at the end ###
slashd=`echo $DESTN | grep /$`
if [ "$slashd" = "" ]; then
DESTN=$DESTN"/"
fi

## Get input from user ##
echo "\n"
echo "Please give a folder name(preferrably your feature) to create under $DESTN"
read feature

cd $DESTN
if [ -d $feature ]; then
echo "\n"
echo "The folder $feature already exists under $DESTN! Do you still want to continue(Y/N)?\c"
read answer
if [ "$answer" != "Y" ]; then
exit
fi
else
echo "\n"
echo "Element creation begins........."
echo "\n"
#cc_mkdir $feature
mkdir $feature
fi

i=1
srcDir=$1
destDir=$DESTN$feature

while [ $i -le $count ]
do

echo "calling CreateElement for first time\n"
CreateElement $srcDir $destDir ""
i=`expr $i+1`

done

echo "\n"
echo "All the Directory and File elements have been successfully created!"
echo "\n"
echo "Please use cc_update -m LATEST to continue with the check-in process......"
echo "\n"

=============================================

Pls. ignore the commented code.

==============
Result

bash-2.05b$ ./ModifiedACT.txt /users/cpati/srcDir/

The default directory to check in your feature code is /users/cpati/
Do you wish to change the destination(Y/N)?N

Please give a folder name(preferrably your feature) to create under /users/cpati/
Ravi

Element creation begins.........

calling CreateElement for first time

Inside CreateElement with Depth -> 3

0-> Dir1_test is the file/dir being processed and 1 -> is j value

1 -> lastKnownDir = Dir1_test/

Inside CreateElement with Depth -> 3

0-> Dir12 is the file/dir being processed and 1 -> is j value

1 -> lastKnownDir = Dir12/

Inside CreateElement with Depth -> 2

0-> file112.cti is the file/dir being processed and 1 -> is j value

2 -> destDir is /users/cpati/Ravi/Dir1_test//Dir12/ in the file handling code

3 -> srcDir is /users/cpati/srcDir//Dir1_test//Dir12/ in the file handling code

4 -> lastKnownDir is Dir12/ in FHC

create element -> file112.cti

5 -> J value at end of loop is 2, before Done

0-> file112.cti is the file/dir being processed and 2 -> is j value

2 -> destDir is /users/cpati/Ravi/Dir1_test//Dir12/ in the file handling code

3 -> srcDir is /users/cpati/srcDir//Dir1_test//Dir12/ in the file handling code

4 -> lastKnownDir is Dir12/ in FHC

File file112.cti already exists!

5 -> J value at end of loop is 3, before Done

3 == 2 moving one directory up !!!!!

lastknownDir at end is -> Dir12/ *****

sed: command garbled: s/Dir12///
sed: command garbled: s/Dir12///
isdir is ->

srcDir -> and destDir ->

Just before return from CreateElement

5 -> J value at end of loop is 4, before Done

4 == 2 moving one directory up !!!!!

lastknownDir at end is -> Dir12/ *****

sed: command garbled: s/Dir12///
sed: command garbled: s/Dir12///
isdir is ->

srcDir -> and destDir ->

Just before return from CreateElement

5 -> J value at end of loop is 5, before Done

5 == 2 moving one directory up !!!!!

lastknownDir at end is -> Dir12/ *****

sed: command garbled: s/Dir12///
sed: command garbled: s/Dir12///
isdir is ->

srcDir -> and destDir ->

Just before return from CreateElement

calling CreateElement for first time

Inside CreateElement with Depth -> 19

0-> 142_unison is the file/dir being processed and 1 -> is j value

Direcotry 142_unison already exists!

1 -> lastKnownDir = 142_unison/

./ModifiedACT.txt: /142_unison/: does not exist

===============================================

Your code shows that you know VERY LITTLE about the UNIX-Commands as well as scripting!!

Even if you complete this ASSIGNMENT (and the course for which this is), it won't provide you with any benefits UNLESS you learn the UNIX-Commands very well.

Nevertheless, I've corrected your code as little as to show you the "problem areas" and they are marked with #AAAAAAAA for added-code and #CCCCCC for changed-code. I hope you see why it wasn't working!?!?

CreateElement() {
    srcDir=$1
    destDir=$2
    lastKnownDir="$3"

    cd $srcDir
    depth=`ls | cut -f1 | wc -l`
    echo "Inside CreateElement with Depth -> $depth \n"

    j=1
    while [ $j -le $depth ]
    do
        a=`ls | cut -f1 | head -$j | tail -1`
        echo "0-> $a is the file/dir being processed and $j -> is j value\n"
        isdir=`ls -p | cut -f1 | head -$j | tail -1 | grep /`
        #echo "1-> isdir = $isdir ,\n"
        if [ "$isdir" != "" ]; then
            cd $destDir
            if [ -d $a ]; then
                echo "\nDirecotry $a already exists!\n"
            else
            #cc_mkdir $a
                mkdir "$a"
            fi
            srcDir=$srcDir$a        #CCCCCCCCCCCC   $srcDir/$isdir
            destDir=$destDir$a      #CCCCCCCCCCCC   $destDir/$isdir
            lastKnownDir="$isdir"
            echo "1 -> lastKnownDir = $lastKnownDir\n"
            CreateElement $srcDir $destDir $lastKnownDir
        else
            cd $destDir
            echo "2 -> destDir is $destDir in the file handling code\n"
            echo "3 -> srcDir is $srcDir in the file handling code\n"
            echo "4 -> lastKnownDir is $lastKnownDir in FHC\n"
            if [ -f $a ]; then
                echo "\nFile $a already exists!\n"
            else
                #cc_mkelem $a
                echo "create element -> $a \n"
            fi
            cp $srcDir$a .
        fi
        j=`expr $j + 1`
        echo "5 -> J value at end of loop is $j, before Done\n"
    done

    if [ $j -gt $depth ]; then
        echo "$j == $depth moving one directory up !!!!!\n"
        echo "lastknownDir at end is -> $lastKnownDir *****\n"
        srcDir=`echo $srcDir | sed "s=$lastKnownDir=="`       #CCCCCCCCCCCCC  -e "s/"$lastKnownDir"//"`
        destDir=`echo $destDir | sed "s=$lastKnownDir=="`     #CCCCCCCCCCCCC  -e "s/$lastKnownDir//"`
    fi

    echo "isdir is -> $isdir \n"
    echo "\nsrcDir -> $srcDir and destDir -> $destDir \n"

    echo "Just before return from CreateElement\n"
    return
}


#DESTN="$IPCBU_AUTO_HOME/scripts/"
DESTN="/users/cpati/"

clear

### Check for number of arguments ###
if [ $# -ne 1 ]; then
    echo "Usage : $0 <Absolute Path of source directory>\n"
    exit
fi

SRC=$1

### Check whether Source path has a / at the end ###
slash=`echo $1 | grep /$`
if [ "$slash" = "" ]; then
    SRC=$SRC"/"
fi

### First level ###
cd $SRC
count=`ls | cut -f1 | wc -l`

## Check for existance of files and dir ##
if [ $count -eq 0 ]; then
    echo "WARNING!!There are no files in the specified source to check in\n"
    exit
fi

## Check for change in destn directory ##
echo "\n"
echo "The default directory to check in your feature code is $DESTN"
echo "Do you wish to change the destination(Y/N)?\c"
read option
if [ "$option" = "Y" ]; then
    echo "Please specify the absolute path of the new destination."
    read DESTN
fi

### Check whether Destination path has a / at the end ###
slashd=`echo $DESTN | grep /$`
if [ "$slashd" = "" ]; then
    DESTN=$DESTN"/"
fi

## Get input from user ##
echo "\n"
echo "Please give a folder name(preferrably your feature) to create under $DESTN"
read feature
#AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
### Check whether feature has a / at the end ###
slashd=`echo $feature | grep /$`
if [ "$slashd" = "" ]; then
    feature=$feature"/"
fi
#AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

cd $DESTN
if [ -d $feature ]; then
    echo "\n"
    echo "The folder $feature already exists under $DESTN! Do you still want to continue(Y/N)?\c"
    read answer
    if [ "$answer" != "Y" ]; then
        exit
    fi
else
    echo "\n"
    echo "Element creation begins........."
    echo "\n"
    #cc_mkdir $feature
    mkdir $feature
fi

i=1
srcDir=$SRC                  #CCCCCCCCCCCCCCC $1
destDir=$DESTN$feature

while [ $i -le $count ]
do
    echo "calling CreateElement for first time\n"
    CreateElement $srcDir $destDir ""
    i=`expr $i+1`
done


echo "\n"
echo "All the Directory and File elements have been successfully created!"
echo "\n"
echo "Please use cc_update -m LATEST to continue with the check-in process......"
echo "\n"

Thanks for your inputs. Yes, am new to shell scripting. The srcDir and destDir variables are working fine now. But still the recursion control variable J is behaving like a Global variable. How do i make "J" as a local variable so that there are copies of the variable J created in every call to createelement? Pls. let me know. Tried "typeset j=0" as the first line of the file but doesn't seem to work.