Sponsored Content
Top Forums Shell Programming and Scripting Recursion to Copy a Directory structure Post 302179755 by unilover on Friday 28th of March 2008 12:02:44 PM
Old 03-28-2008
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!?!?

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$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"

 

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

any idea to copy directory structure

I want to copy the durectory structure having subdirectories in it ... would be greatful if anyone could let me know .. thanks in advance (6 Replies)
Discussion started by: myelvis
6 Replies

2. UNIX for Dummies Questions & Answers

Copying a Directory Structure to a new structure

Hi all Is it possible to copy a structure of a directory only. e.g. I have a file with the following entries that is a result of a find :- /dir1/dir2/file.dbf /dir1/dir2/dir3/file1.dbf /dir1/file.dbf I want to copy these to a directory and keep the structure however starting at a new dir... (8 Replies)
Discussion started by: jhansrod
8 Replies

3. UNIX for Advanced & Expert Users

MV files from one directory structure(multiple level) to other directory structure

Hi, I am trying to write a script that will move all the files from source directory structure(multiple levels might exist) to destination directory structure. If a sub folder is source doesnot exist in destination then I have to skip and goto next level. I also need to delete the files in... (4 Replies)
Discussion started by: srmadab
4 Replies

4. UNIX for Dummies Questions & Answers

copy files with directory structure

i have a text file as. /database/sp/NTR_Update_Imsi_List.sql /database/sp/NTR_Update_Imsi_Range_List.sql /database/sp/NTR_Vlr_Upload.sql /database/tables/StatsTables.sql /mib/ntr.mib /mib/ntr.v2.mib /scripts/operations/ntr/IMSITracer.ph /scripts/operations/ntr/IMSITracer.pl ... (3 Replies)
Discussion started by: adddy
3 Replies

5. Shell Programming and Scripting

copy directory structure to a system on the network

I am trying to write a script which has to copy the directory structure from my system to another system on the network. But I dont want the files to be copied. I think I have to start with copying all subdirectories names in a directory to a system on the network. Here's the case: Source... (1 Reply)
Discussion started by: firefox211
1 Replies

6. Shell Programming and Scripting

Need to copy a directory structure from one server to another

Hello All, I have got a dev server and a production server will call it as D server and P server. I have a dir structure in my D server and i need to create the same dir structure in P server also using a shell script. If i do a find . in my D server, i am getting an o/p like :- . ./vio... (9 Replies)
Discussion started by: Renjesh
9 Replies

7. Programming

Deep copy of structure in C

Hi , I have a scenario where i need to copy the iter to another local variable , where iter is of type MCC_T_SYS_ADDRINFO *iter . struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; ... (5 Replies)
Discussion started by: breezevinay
5 Replies

8. Shell Programming and Scripting

Directory Tree/Recursion Issue . . .

Hope the title is close enough; didn't quite know how to put this... OK. We have a hypothetical script, script.a, with one simplistic line:bash ./nextDir/script.bNow script.b has a bit more going for it; and does file moves, copies, and renames, calling all the targets with respect to itself.... (7 Replies)
Discussion started by: LinQ
7 Replies

9. UNIX for Beginners Questions & Answers

Copy directory withOUT recursion

Hi, I cannot find a way to copy a directory to another location with all attributes (mode, ownership, timestamps) but withOUT recursion (after so many years of working with Linux). Say I want to create /home/jail/tmp exactly like /tmp but with nothing in it. Here is what I tried: ... (7 Replies)
Discussion started by: chebarbudo
7 Replies
All times are GMT -4. The time now is 09:42 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy