Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

2 loops, 1 if statement and 1 script 1

Status
Not open for further replies.

chris01010

Programmer
Jan 29, 2004
25
GB
I am currently writing a script where it is necessary to copy a number of files to a number of directories and I want to do it re-using 1 if statement, what I don't want is to copy all of the files to all the directories.

Basically I want the script to look at the first variable (being the filename) and the second variable (namely the directory)then execute the if statement which copies the file to the directory. Then loop to the next set of filenames and directories and repeat the if statement.

I have managed to get as far as having all files being copied to each of the directories that it loops through.

Does anyone have any suggestions on how to get the desired result?
 
posting a sample code could help ;)

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
what is the if condition going to check ?

anyways -
suppose you have a file called lines
with a filename and dirname in each line
like
file1 dir1
file2 dir2
then
---
for line in `cat lines`
do
set $line
file=$1
dir=$2
# if [ whatever your condition is ]
# i will check if file exists
if [ -f $dir/$file ]
then
cp $file $dir
fi
done
---
of course you can make it sophisticated like
if [ -f $dir/`basename $file`]
etc
 
for file in `cat billdel`

do
set $file


if [ -f ${HOME}/${release}/${build}/$file ]

then

for dir in `cat billdir`

do
set $dir

scp ${HOME}/${release}/${build}/$file ${user}${node}:$dir

done

else

echo &quot;file $fileor target directory doesn't exist&quot;

fi


done


The above is an example of the code that i'm currently using. Unfortunately at the moment, the outer loop updates the file variable and then copies the file to all the dir variables specified in the inner loop. I need the two loops to work simultaneously, where the first file in the outer loop is copied to the first directory in the second loop, and then the script goes back to the outer loop updates the file variable to the next filename and then goes to the inner loop and copies to the second directory.

I hope this makes sense :) Any ideas would be brilliant.
 
Try something like this :
[tt]
paste billdel billdir | while red file dir
do
if [ -f ${HOME}/${release}/${build}/$file ]
then scp ${HOME}/${release}/${build}/$file ${user}${node}:$dir
else echo &quot;file $file or target directory doesn't exist&quot;
fi
done
[/tt]

Jean Pierre.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top