Script to FTP a modified file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to FTP a modified file
# 1  
Old 01-20-2012
Script to FTP a modified file

Hello,

I am fairly new to shell scripting. I see a lot of examples out there of how to find if a file has been modified within a certain period of time. What I'm looking for help with is a script that will run and I'm thinking check for the last 24 hours but if not just check at runtime to see if an HTML file has been modified.

Then once it determines if the file has changed (I've read this can be done with either checksum or the md5 or md5sum commands) then it will FTP the file. I already have an example of how to connect to an FTP and transfer a file but again since I'm new to shell scripting I'd like some help with this.

On another forum I found an example of checking for if a file has been modified and printing things out. Since this will run in the background of a website I don't need it to print but I'm going to post the code here:
Code:
#!/bin/sh 
# 
# 
# MD5FILE-parameter specifies where we want to save our md5print for 
# later use. 
MD5FILE=/tmp/.md5savefile 

# The FILE_TO_CHECK-parameter specifies the file we want to monitor 
# changes 
FILE_TO_CHECK=/tmp/filetocheck 

if [ ! -f $FILE_TO_CHECK ] 
then 
echo "ERROR Couldnt locate file to check:$FILE_TO_CHECK" 
exit 1 
fi 

echo "Taking a print on $FILE_TO_CHECK with md5sum" 
MD5PRINT=`md5sum $FILE_TO_CHECK | cut -d " " -f2` 

if [ -z $MD5PRINT ] 
then 
echo "ERROR Recived an empty MD5PRINT thats not valid, aborting" 
exit 1 
else 
echo "MD5PRINT we got was:$MD5PRINT" 
fi 

if [ -f $MD5FILE ] 
then 
echo "Found an old savefile:$MD5FILE we trying to match prints" 
OLDMD5PRINT=`cat $MD5FILE` 

if [ -z $OLDMD5PRINT ] 
then 
echo "Got an empty string from the oldfile, aborting" 
exit 1 
fi

if [ "$OLDMD5PRINT" = "$MD5PRINT" ] 
then 
echo "New and old md5print are identical, the file hasnt been changed" 
else 
echo "WARNING the old and new md5print doesnt match, the file has been changed" 
fi 

fi 

echo "Saving to new md5print in logfile:$MD5FILE for later checks" 
echo $MD5PRINT > $MD5FILE 

if [ $? = 0 ] 
then 
echo "Wrote to file OK" 
else 
echo "Writing to file failed...why??" 
exit 1 
fi

I'm looking to modify this code to fit my needs. Any help would be greatly appreciated.

Thanks


Moderator's Comments:
Mod Comment How to use code tags

Last edited by Franklin52; 01-20-2012 at 12:13 PM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 01-20-2012
Hi,

the script performs various checks on the given file with full path stored in FILE_TO_CHECK variable.

The value of last MD5 check is stored in the file referenced by variable MD5FILE.

If you don't want to print messages comment or delete the lines containing the command echo followed by the messages.

The checks are in order:
1. check if file exists;
2. check for an empty md5sum (nothing to compare);
3. check if a file containing a previous md5 sum value exists;
3.1 check if old value of md5sum is void;
3.2 check if old and new md5sum value are identical:
if true: perform something (in your case nothing should be performed)
else: perform some other thing (in your case you should insert the instructions to ftp the file).

see ya
fra
# 3  
Old 01-20-2012
Quote:
The checks are in order:
1. check if file exists;
2. check for an empty md5sum (nothing to compare);
3. check if a file containing a previous md5 sum value exists;
3.1 check if old value of md5sum is void;
3.2 check if old and new md5sum value are identical:
if true: perform something (in your case nothing should be performed)
else: perform some other thing (in your case you should insert the instructions to ftp the file).
I have a few questions about this. Again I am really new to shell scripting and I'm sure I could look up how to do each of these steps but if you could give me some examples that would be great.

Thanks,
Pyro
# 4  
Old 01-20-2012
You don't have to "look up how to do each of these checks". He's describing in detail what the code you posted does.

To see the code for it, read the code.
# 5  
Old 01-20-2012
Quote:
On another forum I found an example of checking for if a file has been modified and printing things out. Since this will run in the background of a website I don't need it to print but I'm going to post the code here:
Posting someone else's code and asking us to modify it to fit your vague specification is not what this forum is about. Let's see some effort on your part.
Let's imagine that you offered one kilo of gold to a company to write this software but they would only write what was in the specification. Have you provided enough information?
# 6  
Old 01-23-2012
I apologize. I think what I was given earlier is a great help. To be more specific we have an HTML file that is being output by a Java class. That HTML file will reside in a folder and the Java to create the file will be run on a recurring basis. We just want to see if something changes in the HTML file and if it does we FTP it over to another server and that's what this script is supposed to do when the Java class calls it.

---------- Post updated at 11:36 AM ---------- Previous update was at 08:38 AM ----------

I found an issue with the script. It copies the file no matter what. I tested this by grabbing a new HTML file that has not been changed at all and ran the script and it copied it. Below is my new code:

Code:
#!/bin/sh
#
#
# MD5FILE-parameter specifies where we want to save our md5print for
# later use.
MD5FILE=/tmp/.md5savefile

# The FILE_TO_CHECK-parameter specifies the file we want to monitor
# changes
current_date=`date +%Y%m%d`
FILE_TO_CHECK=MSN.htm

if [ ! -f $FILE_TO_CHECK ]
then
echo "ERROR Couldnt locate file to check:$FILE_TO_CHECK"
exit 1
fi

echo "Taking a print on $FILE_TO_CHECK with md5sum"
MD5PRINT=`md5sum $FILE_TO_CHECK | cut -d " " -f2`

if [ -z $MD5PRINT ]
then
echo "ERROR Recived an empty MD5PRINT thats not valid, aborting"
exit 1
else
echo "MD5PRINT we got was:$MD5PRINT"
fi

if [ -f $MD5FILE ]
then
echo "Found an old savefile:$MD5FILE we trying to match prints"
OLDMD5PRINT=`cat $MD5FILE`

if [ -z $OLDMD5PRINT ]
then
echo "Got an empty string from the oldfile, aborting"
exit 1
fi

if [ "$OLDMD5PRINT" = "$MD5PRINT" ]
then
exit 1
else
cp MSN.htm  MSN.$current_date.htm
fi

fi

echo "Saving to new md5print in logfile:$MD5FILE for later checks"
echo $MD5PRINT > $MD5FILE

if [ $? = 0 ]
then
echo "Wrote to file OK"
else
echo "Writing to file failed...why??"
exit 1
fi

# 7  
Old 01-23-2012
In testing it to see what's going wrong, the script has logic errors. It's comparing a file name to a checksum, and that makes no sense at all... It's also exiting with failure places I'd expect it to be returning success. It's also not bothering to use a lot of its own predefined variables and just using hardcoded names in places.

You don't need to read raw md5sums out of the file anyway, md5sum can verify things by itself.

Code:
#!/bin/bash

# If we have no /tmp/.md5sum
if [ ! -f /tmp/.md5sum ]
then
        echo "Generating new checksum file" >&2
        md5sum /path/to/file > /tmp/.md5sum
        # Return error if .md5sum couldn't be created
        [ -f /tmp/.md5sum ] || exit 1
        exit 0
fi


# Check if file has changed, quit if it hasn't.
md5sum -c /tmp/.md5sum && exit 0

echo "File has changed, do things here" >&2

# ...
# ... ftp and stuf
# ...

echo "Generating new checksum file" >&2
md5sum /path/to/file > /tmp/.md5sum

exit

It might even be possible to check for multiple files this way, by doing md5sum file1 file2 file3 ... > /tmp/.md5sum, though you'd have to start processing the output of md5sum -c, not just reading its return value, to tell which files didn't match.

/tmp/.md5sum might not be the best place to store this file. Your homedir may be better.

Last edited by Corona688; 01-23-2012 at 02:23 PM..
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

shell script to alert if a file has been modified

Hi , I want a script who will send alert the moment someone edit any file in a directory in LINUX. Can some one throw some light on this please.!! (4 Replies)
Discussion started by: d8011
4 Replies

2. Shell Programming and Scripting

Script to check for the file existence, if file exists it should echo the no of modified days

Hi, I am looking for a shell script with the following. 1. It should check whether a particular file exists in a location #!/bin/sh if ; then echo "xxx.txt File Exists" else echo "File Not Found" fi 2. If file exists, it should check for the modified date and run a command... (2 Replies)
Discussion started by: karthikeyan_mac
2 Replies

3. Shell Programming and Scripting

Run a script when a file is modified/time stamp changed

Hi, I need to run a script file which uses a file and that file is modified as and when some alarms generated, it is not based on any fixed time period.. it may be modified even once in a minute for some time and once in 30 min or once in 20 min. Hence i need to watch for the timestamp change of... (3 Replies)
Discussion started by: aemunathan
3 Replies

4. Shell Programming and Scripting

FTP files modified after a particular date between servers

Hi all, i need to write a shell script to transfer a file modified after a particular date from one server to another. I searched for the related posts in this forum and got hints and snippets for it. i tried the below code ftp serverA user uname pwd lcd to_dir cd from_dir files=$(find... (7 Replies)
Discussion started by: mick_000
7 Replies

5. UNIX for Dummies Questions & Answers

Run a script if file modified in last 10 min

Hi, I want to check if a file is modified or not in the last 10 mins and run a script if so. Thanks (8 Replies)
Discussion started by: krabu
8 Replies

6. Shell Programming and Scripting

How to get a filename modified by attaching modified timestamp

Hi, I want to modify a filename in AIX by attaching the last modified timestamp. I want the timestamp completely in numerical format (eg:200905081210. yr-2009, mnth - 05, date -08, hr - 12, mins - 10). For example if the filename is a.log and it was modified on April 6th 2008 at 21.00. I... (16 Replies)
Discussion started by: Ruks
16 Replies

7. Shell Programming and Scripting

how to write a shell script that print the last modified file ?

Hi guys, -could any one help me with this (I'm new to UNIX) how to write a shell script that tell me the last modified file in the current directory? so if I run the script in a diferent directory,will work. and can I write the script by C++ language and run it in the shell ? I tried... (5 Replies)
Discussion started by: FunnyWolF
5 Replies

8. Shell Programming and Scripting

FTP script to FTP file to UNIX - Solaris

Hello, A couple of times per week, i receive emails notifications when files are available for processing. Currently i read these eamails with a java program and store the attachement on my C: drive and would now like to generate a PC script to send this file name up to UNIX-Solaris and... (3 Replies)
Discussion started by: bobk544
3 Replies

9. UNIX for Dummies Questions & Answers

how to retrieve original contents of a modified file (modified using vi)

Made changes to a file using vi editor and saved those changes now realised that the changes are not required How can I get the previous version of the file.i.e the one which was there on which I had made changes (3 Replies)
Discussion started by: novice100
3 Replies

10. Shell Programming and Scripting

ftp most recently modified file

Hi what is the most optimum way to ftp the most recently modified file starting with a particular string. i tried this ftp -n 2>logfile 1>&2 <<EOF open xxxxxx user xxxx xxxx prompt ls -ltr f* res !var=`tail -1 |awk { print $9 }'` bye EOF that gives... (6 Replies)
Discussion started by: ahmedwaseem2000
6 Replies
Login or Register to Ask a Question