If File has been updated, do something??


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting If File has been updated, do something??
# 1  
Old 11-06-2008
If File has been updated, do something??

Put this together from somewhere else on the forums, just modified it and added the loop.


Code:
#!/bin/ksh

localFile=$1
remoteFile=$2

#source FTP parameters
. .ftp_put.cfg

mylog=ftp_session.log
echo "$(date "+%H:%M:%S") - Attempt to FTP $1 to $2" > $mylog
machine="server1 server2 server3 server4"
count=0
# do the FTP put

for machine in $machine
do
ftp -i -n <<EOF >> $mylog
open $machine
user $FTP_LOGIN $FTP_PASSWORD
put $localFile $remoteFile
ls $remoteFile
quit
EOF
count=`expr $count + 1`
done

This script gets called by another script through a cron job every 5 minutes:

Code:
/tmp/abcQATest/abcMoveTest.sh archive.tar /tmp/archive.tar

Anyways, I was wondering if there was any way of making it a little better. Currently this simply ftp's that static file every 5 minutes, regardless of if the file has been updated or not. The way it works now is if someone needs to make an update to the 4 server's that this script FTP's to, they simply drop their archive file(named archive.tar) to the /tmp/abcQATest/ directory, and then within 5 minutes the crontab runs and ftp's their file to server 1-4 at the /tmp/archive.tar location.

Is there any way to set this up so it only ftp's the file if it's been updated since the last time the ftp ran? That way, if it gets updated, it get's FTP'd once, but then it doesn't get FTP'd again unless the file's been changed?

Then on the other 4 servers, I also need something like this:

Code:
If /tmp/archive.tar has been updated

do something
.
.
else
endif

The do something part is just a couple quick moves, extracts and deletes, that's no big deal. It's If statement I am stumped on.
# 2  
Old 11-06-2008
cat file1 > file2

lets say file1 gets updated.

diff file1 file2 > file3.

if [ -s file3 ]
do something. (in your case FTP)
fi

thanks
Sumeet
# 3  
Old 11-06-2008
Script:

Code:
diff archive.tar backup.tar > tmp
if [ -s tmp ]; then
/tmp/abcQATest/abcMoveTest.sh archive.tar /tmp/archive.tar
echo "Files different, transferring files"
else
echo "Files the same, exiting";
fi

Results:

Code:
/tmp/abcQATest>./autoMoveScript.sh
Binary files archive.tar and backup.tar differ
Files the same, exiting
/tmp/abcQATest>

The binary files do differ, but then for some reason it always goes to the second part of the if statement.

Last edited by cbo0485; 11-06-2008 at 05:17 PM..
# 4  
Old 11-06-2008
I believe you have to use the -a parameter on the diff command to compare binaries.
# 5  
Old 11-07-2008
Quote:
Originally Posted by BubbaJoe
I believe you have to use the -a parameter on the diff command to compare binaries.
It worked, but now even if archive.tar and backup.tar are the same, it says their different and it runs the first block, instead of just doing the echo which states "Files the same, exiting";

Code:
diff archive.tar backup.tar > tmp
if [ -a tmp ]; then
/tmp/abcQATest/abcMoveTest.sh archive.tar /tmp/archive.tar
echo "Files different, transferring files"
cat archive.tar > backup.tar
else
echo "Files the same, exiting";
fi

# 6  
Old 11-07-2008
Here's my final code I got working.


Code:
cmp archive.tar backup.tar > /dev/null
if [[ $? -eq 1 ]]; then
/tmp/abcQATest/abcMoveTest.sh archive.tar /tmp/archive.tar
echo "Files different, transferring files"
cat archive.tar > backup.tar
else
echo "Files the same, exiting";
fi

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Log File updated time

hi can any one please help on below .im new to shell scrpting i need to write a shell script which will check particular log file is presented or not in specific location ,if yes how long it was not modified/not rolling ?if its not modified/log is not rolling script will have to send mail (9 Replies)
Discussion started by: 4Learning
9 Replies

2. UNIX for Dummies Questions & Answers

Copy the last part since the file has been updated

Input File1 constatntly running and growing in size. My Program Erorr ddmmyy hh:mm:ss My Program Error **Port 123 terminated **ID PIN 12345 Comamnd Successful Command Terminated Command Successful Command Terminated **My Program Erorr ddmmyy hh:mm:ss My Program Error **Port 345... (3 Replies)
Discussion started by: eurouno
3 Replies

3. Shell Programming and Scripting

Is there any command to know what updated last in file?

Is there any command to know what updated last in file? (3 Replies)
Discussion started by: anuragpgtgerman
3 Replies

4. Shell Programming and Scripting

Search for a recently updated file

Hi, I am looking for a command to search for a specific file which was recently modified in the current directory leaving some unwanted files to be listed. For example, when I try ls - lrt it shows the following output. I want to ommit the files with the name 'resend' and... (3 Replies)
Discussion started by: svajhala
3 Replies

5. Shell Programming and Scripting

trying to check if the file is getting updated or not

#!/bin/sh #set -x Current_Date=`date +"%b %e"` Filepmdate=`ls -ltr /file/ | tail -5 | awk '{print $6,$7}'` if ; then echo " " exit 0 else echo "Log files are not updated please check" exit 0 fi done > sh -x l12.sh + + date +%b %e Current_Date=Aug... (2 Replies)
Discussion started by: arch12
2 Replies

6. Shell Programming and Scripting

Problem updated file with new entries

Hi, I have a file, fileA, that consists of two fields. Field 1 has an old object name, and field 2 has a new object name. I would like to search and replace a master file, FileB, by substituting the old object name (field 1 in fileA) with the new object name ( field 2 in fileA). FileA:... (16 Replies)
Discussion started by: pitccorp01
16 Replies

7. Programming

Log file not getting updated

hi all, i'm a student and managing lab at my insti. there in one application in which log file has to be maintaine the number of bytes transferred and received. but after certain entries these two attributes stop getting updated and holds same values for rest of the session. This happens one time... (4 Replies)
Discussion started by: KornFire
4 Replies

8. UNIX for Dummies Questions & Answers

Find last updated file

Hi all, my problem is teh following: I need to move a file from a folder to another and I usually do it by the get command but in this case I have a list of file in the source folder and I have to select only the lust updated one. Ho to do this? All the files have the same name followed... (4 Replies)
Discussion started by: callimaco0082
4 Replies

9. Shell Programming and Scripting

checking out latest updated file

hi, i was wondering if there is a way to find out which file is updated the most recent time in the entire file system.... most probably this would be refering to some LOG file, but i wanna find a way which file was that... some script should work or what? can anybody help me out on that. for... (3 Replies)
Discussion started by: asadlone
3 Replies

10. Shell Programming and Scripting

Creating an updated file

Hi, I would like to define a script in order to update a file with the last updated records: I wrote : #!/bin/sh YEAR=$(date +%Y) MONTH=$(date +%m) DAY=$(date +%d) COMM=/usr/bin/comm for file in * ; do if ] ; then FILE=${DPSTY} UNIQUE=Unique_${FILE} ... (4 Replies)
Discussion started by: dbfree
4 Replies
Login or Register to Ask a Question