Could someone help with setting loops?

Hi,

Not the greatest scripting abilities here. I am trying to automate a process that we do daily at work but its been a while for me since i used a loop command.
Layout of process:

Someone sends us a TAG number to be promoted.
we create a temp directory and extract the tag into that directory. then we would look for the file confirm that in the target location the file does not exist, if it does we back it up by renaming it as file.B(date).
sometimes there are 1 files sometimes there are 10. something for the directories. the one file could go into many directories or just one. Here is what i have so far.

I think I'm on the right path but I need to loop when it ask "what file do we need to check out" to continue to ask until we type "done"
(This is prob. not the best description but I'll try my best to clarify anything that is not making sense.) I have put comments for area's i'm stuck in. :wall:

#!/bin/bash

DATE=$(date +"%m%d%y%H%M")


#Name of the Dir that needs to be created for CVS checkout

read -p "What is the check out directory? : " dir1
read -p "What is the name of the Tag? : " tag

#This line needs to be looped. Modify script, when you type "done" you are finished with the files to check out.
#There could possibly be 1-10 files to be promoted.
read -p "What is the name of the file in the checkout directory? : " file1
read -p "What is the name of the file in the checkout directory? : " file2

#etc.

#This line needs to be looped. Modify script, when you type "done" you are finished listing the directories this file has to go into.
#There could possibly be 1-20 target locations to be promoted.
read -p "What is the target directory for $FILE1 to be promoted to? : " tar1
read -p "What is the target directory for $FILE2 to be promoted to? : " tar2

#etc

#This line is going to start the copy and move process for promotion:

#Create the directory
/usr/bin/mkdir $PWD/$DIR1

#Check out the tag for promotion:
cvs co -r $TAG $DIR1

#find the file and the path inside the "dir1" to be copied and promoted:
#This line will need to be looped as well to follow how many files need to be promoted.
find $DIR1 -name $FILE1 #Not sure how to capture the the path once it finds the location of the file and create a new variable.
find $DIR1 -name $FILE2
#etc


#Check to see if the file exist in the target directory: 
if [ -e $TAR1/$FILE1 ];
then
echo "$FILE exists"
echo "Moving to $FILE1.B$DATE"
/usr/bin/mv $TAR1/$FILE1 $TAR1/$FILE1.B$DATE
/usr/bin/cp $FILE1 $TAR1/$FILE1 #File1 will need to be replaced with the new variable.
echo "file has been copied:"
/usr/bin/ls -alt $TAR1|head -4
else
/usr/bin/cp $FILE1 $TAR1
echo "file has been copied:"
/usr/bin/ls -al $TAR1|grep $FILE1
echo "Done!"
fi

Here is a start (it's #!/bin/sh, but it should run with bash too):

#!/bin/sh

FS='|'
file=; while [ "$file" != 'done' ]; do
	[ "$file" ] && files="$files$file$FS"
	read -p 'What is the name of the file in the checkout directory?' file
done

IFS="$FS"; for file in $files; do 
	read -p "What is the target directory for $file to be promoted to?" tar
	[ "$tar" = 'done' ] && break
	[ "$tar" ] && tars="$tars$tar|"
done
exit 0

For the rest, I don't really understand all the requierments :stuck_out_tongue:

1 Like

thanks! solved one problem lol. I'm trying to fix the rest of it but this works great thank you.