views:

40

answers:

3

I wrote this piece of code this morning.
The idea is, a text file (new.txt) has the details about the directory structure and the files in the directory.
Read new.txt, create the same directory structure at a destination directory (here it is /tmp), copy the source files to the corresponding destination directory.

Script

clear    
DEST_DIR=/tmp    
for file in 'cat new.txt'    
do    
   mkdir -p $file    
   touch $file    
   echo 'ls -ltr $file'    
   cp -rf $file $DEST_DIR    
   find . -name $file -type f    
   cp $file $DEST_DIR    
done    

Contents of new.txt

Test/test1/test1.txt    
Test/test2/test2.txt    
Test/test3/test3.txt    
Test/test4/test4.txt    

The issue is, it executes the code, creates the directory structure, but instead of creating it at the end, it creates directories named test1.txt, test2.txt, etc. I have no idea why this is happening.


Another question: For Turbo C, C++, there is an option to check the execution flow? Is there something available in Unix, Perl and shell scripting to check the execution flow?

A: 

you might want to try something like rsync

ghostdog74
+1  A: 

The script creates these directories because you tell it to on the line mkdir -p $file. You have to extract the directory path from you filename. The standard command for this is dirname:

dir=`dirname "$file"`
mkdir -p -- "$dir"

To check the execution flow is to add set -x at the top of your script. This will cause all lines that are executed to be printed to stderr with "+ " in front of it.

schot
Thank you very much Schot. Thats helpful.
Invincible
A: 

Hello all,
Find below the modified code of my program.

clear
set -x
DEST_DIR=/tmp
for file in cat new.txt
do
find . -name $file -type d
dir=dirname "$file"
mkdir -p -- "$dir"
touch $file
echo ls -l $file
find . -name $file -type f
echo cp $file "$dir"
done

Is this correct?
Still, I there is a bug left in this program. It creates directory structure and creates files as well. While I'm trying to copy the contents from source to destination, it copies nothing.
Is the line 'cp $file $DEST_DIR' correct?
Also, it creates duplicate files in the DEST_DIR. If my question looks really idiotic, please forgive me. I swear, I'm new to programming industry. I'm not trained in Programming languages.

Thanks in advance,

Vijay

Invincible
Posting a revised question as an answer doesn't work well; you should either [edit your question](http://stackoverflow.com/posts/3465434/edit) or [post a new one](http://stackoverflow.com/questions/ask). You also probably want to ask a separate question for the part at the end: "For Turbo C, C++, there is an option to check the execution flow? Is there something available in Unix, Perl and shell scripting to check the execution flow?"
Michael Mrozek