Operating System - HP-UX
1753776 Members
7308 Online
108799 Solutions
New Discussion юеВ

Re: mini-document management script. IF statement question

 
SOLVED
Go to solution
rmueller58
Valued Contributor

mini-document management script. IF statement question

Wanted to check and see whether someone might have some insights on how to write a script that will copy files from one location to a second location.

I have a source and a destination.

If the file exists on source and destination with the same name, check to see if the source is newer. if the source file is newer overwrite the destination..

I think the statement would look like this:


if [ "$source" -nt "$destination" ]
then
cp $source $destination
else
echo Destination $destination is a newer file
fi


I define a bunch of variables that set precedent for values in the $source and $destination..
These are CIFS mounted file systems. Scripting solution on Un*x that mounts the file systems in questions and copies from Location A to Location B.

I think I may have my nesting wrong in my scripting
any ideas, suggestions or methods would be appreciated greatly..

5 REPLIES 5
James R. Ferguson
Acclaimed Contributor
Solution

Re: mini-document management script. IF statement question

Hi:

> I think I may have my nesting wrong in my scripting

Syntatically this is ok. What makes you think this is wrong?

Regards!

...JRF...
Mark McDonald_2
Trusted Contributor

Re: mini-document management script. IF statement question

I would try something like

cd source_dir
for $source in `ls`
do
if $source does not exist in dest_dir
then copy it
else
if [ "$source" -nt "$destination" ]
cp $source $destination
else
echo Destination is newer
fi
fi
done

this could probably be done with "elif" too?
Mark McDonald_2
Trusted Contributor

Re: mini-document management script. IF statement question

Sorry, my reply lost the indentation.
rmueller58
Valued Contributor

Re: mini-document management script. IF statement question

James,

I've not use the -nt argument before, we have a web server that allows for job applicants to attach documents, the script takes files from a web server folder and moves them to a CIFS mounted file system for viewing from a 4GL application. often times the copy will bomb out.. or miss a file..

There is a process where we also copy files back to the webserver, the HR clerk will load files into a staging area, and I will list the files and copy them. Last night she loaded 27 files and 6 were missed. For the I create directories if non exist, before the move. What was supposed to have been a temporary work around is now a production must have and it isn't meeting my reliability wishes.

Below is one of the problem scripts. I am wandering it the reliability issues is related to a CIFS issue. Right now all files are moved. I'd like to do only those of "today's date"
That is would reduce network over head and perhaps eliminate timeing issues.


#!/bin/bash
#/bin/umount /mps-docs
#/bin/mount /mps-docs
echo Check mount
export p1=/upload_resumes/
export p2=/upload_resumes/backup/
export wst=/wst-docs/applicant/
export mps=/mps-docs/mps-docs/applicant/
export esu=/esu-docs/applicant/
export plv=/plv-docs/
#export ral=/ral-docs/applicant/
date > /tmp/movetime.log
echo "MOVE LOG " > movelog.log
echo NEW
for fn in `ls /upload_resumes/`
do
for dist in `echo $fn |awk '{print substr($1,1,3)}'`
# DISTRICT SPECIFIC INFORMATION
do
export dist

case $dist in
wst)
export distpath="$wst"
;;
mps)
export distpath="$mps"
;;
esu)
export distpath="$esu"
;;
plv)
export distpath="$plv"
;;
#ral)
#export distpath="$ral"
#;;
*)
;;
esac


#APPID SPECIFIC INFORMATION
for appid in `echo $fn |awk -F_ '{print $2}'`
do
echo FILENAME: $fn
export source="$p1$fn"
echo $source
export destination="$distpath$appid/$fn"
echo DONE

if [ -f "$destination" ]
then
echo "$destination" Exists
#rsync -uv $source $destination
else



if [ -d "$distpath$appid" ]
then
#cp -f $source $destination
rsync -uv $source $destination
#
# File Checking logging #
ls -la $source > /tmp/movesource.log
ls -la $destination > /tmp/movedestination.log
echo "Source Files" >> /tmp/movelog.log
cat /tmp/movesource.log >> /tmp/movelog.log
echo "Destination Files" >> /tmp/movelog.log
cat /tmp/movedestination.log >> /tmp/movelog.log
#
#
#################################################
else
mkdir $distpath$appid
mkdir $distpath$appid/Transcripts
fi

fi
#export backup="$p2`date +%Y%m%d`$fn"
ls $distpath > /tmp/"$dist"path

#cp -f $source $destination
#END OF APPID SPECIFIC INFORMATION
done

#END OF DISTRICT SPECIFIC FILE MANIPULATION
done
# END PROGRAM
done
rm -f /tmp/*path
cd /root
date >> /tmp/movetime.log
echo "File Completion Summary " >> /tmp/movelog.log
cat /tmp/movetime.log >> /tmp/movelog.log
echo "File Contents Summary" >> /tmp/movelog.log
echo ############################################# >> /tmp/movelog.log
mail -s "Move Log `date` " address@domainname.TLD < /tmp/movelog.log


rmueller58
Valued Contributor

Re: mini-document management script. IF statement question

Found resolution and re-wrote script, to eliminate dups and redundancy.