Could someone help with setting loops?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Could someone help with setting loops?
# 1  
Old 07-09-2012
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. Smilie

Code:
#!/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

# 2  
Old 07-09-2012
Here is a start (it's #!/bin/sh, but it should run with bash too):
Code:
#!/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 Smilie
This User Gave Thanks to tukuyomi For This Post:
# 3  
Old 07-10-2012
thanks! solved one problem lol. I'm trying to fix the rest of it but this works great thank you.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Need help with for loops

Why wont my for statements work? Im trying to get this script to swich to a user an if you put in a start/stop/or restart paramater to do just that for each user. I commented out the actual start/stop actions to test it just by using echos and not do anything hasty in the environment but it... (0 Replies)
Discussion started by: LilyClaro
0 Replies

2. Solaris

Is there a difference between setting a user as nologin and setting it as a role?

Trying to figure out the best method of security for oracle user accounts. In Solaris 10 they are set as regular users but have nologin set forcing the dev's to login as themselves and then su to the oracle users. In Solaris11 we have the option of making it a role because RBAC is enabled but... (1 Reply)
Discussion started by: os2mac
1 Replies

3. Shell Programming and Scripting

Loops

Hi All, I am very new to Shell scripting. I read basic scripting manual. But i didn't understand the code. Please tell the meaning of the below code: while getopts "F:f:R:r:C:c:" opt 2>/dev/null do case ${opt} in F|f) FREQUENCY_MODE=$OPTARG;; ... (3 Replies)
Discussion started by: pdathu
3 Replies

4. Shell Programming and Scripting

Help with loops

I am trying to do ftp of some crt files that have spaces in the file names. I have to do approximately 4500 files ftp'ed and then check whether the trasnfered file size matches the source files. This has to be in a loop I mean 1. ftp the file, check for file size whether the source and target... (0 Replies)
Discussion started by: dsravan
0 Replies

5. UNIX for Dummies Questions & Answers

Help with for loops

Hi, I am starting to enhance my scripting knowledge and need some assistance with simple 1 line for loops. This may help to do a mass permissions change on a big apache doc root while I have an insane customer on my phone. What is the best resource to learn this skill and what are some that... (5 Replies)
Discussion started by: Oshie74
5 Replies

6. UNIX for Dummies Questions & Answers

For Loops

Hi everyone, I have a question regarding for loops. I am writing a BASH shell script that will contain multiple loops. These loops all involve looping with a count: for (( a=0; a <=10; a++ )); do echo $a done If none of my multiple loops relate to each other throughout the script,... (3 Replies)
Discussion started by: msb65
3 Replies

7. Shell Programming and Scripting

Help with the 2 for loops

#!/bin/bash IFS=$'\n' A= a c b t g j i e d B= t y u i o p counter=0 found="" for i in $(cat $A) do for j in $(cat $B) do if then found="yes" fi done if then (1 Reply)
Discussion started by: vadharah
1 Replies

8. Shell Programming and Scripting

while loops

Hi I've a file like so: Now, I want to read my file and take ex. the Media ID and the Type for each groups of Media (Media1,Media2,...,Media(n): cat /tmp/file|\ while read FILE do while $(FILE|cut -d: -f1)=Media$i do #here will be some test, ex: #if Media ID < 23 ... (4 Replies)
Discussion started by: nymus7
4 Replies

9. UNIX for Dummies Questions & Answers

two loops

Hi, how can I use "for" to have two loops : this is my script : for i in (A B C) do for j in (a b c) do echo $i$j done done #End I want to print out Aa Ab Ac .... But I have error message : syntax error at line 1 : `(' unexpected Many thanks before. How should I use "for" ?? (2 Replies)
Discussion started by: big123456
2 Replies

10. Shell Programming and Scripting

no more loops

Hello Guys I need some help with my script I'm not very good at this, hope you can point me in the right direction. My idea, to write a file with some router commands and use it on a script for me to run on a demand basis, ( or added to the cron, if I get it to work). The below script works,... (5 Replies)
Discussion started by: tony3101
5 Replies
Login or Register to Ask a Question