Not the usual ftp script question

I have an order file that gets updated periodically on a ftp server.
The file format is "number one-day of the quarter-file number". For example,
the format would look like this for today and would increment as such.

1-26-1
1-26-2
1-26-3
etc

After I grab each oder file, I will rename it 1-26-1.done, 1-26-2.done, 1-26-3.done, and so on.

My qusetion is how to grab an order file, rename it to done, then when the script runs again, pull the next sequential file without touching the done file.

Also, if there are multiple new order files, how can I grab multiple files and rename them during one interactive ftp session.

Any help would be greatly appreciated.

The following script doesn't match your problem exactly as I was using ssh/scp with RSA key equivalency between the hosts, but it should illustrate a possible method.

for i in `ssh -p 22 user@host find /some/path/to/files/ -name ! *.done -mmin +1`; do
        /usr/local/bin/scp -P 22 user@host:$i /where/I/want/the/files
        /usr/local/bin/ssh -p 22 user@host mv -f $i ${i}.done
done

The same idea could be extened into a script with ftp.

Cheers,

Keith

Thank you for the response. This would be easy if the ftp server wasn't Windows; however, the ftp server is windows, and the ftp script is running from a unix server using the bash shell.

You should consider switching to ksh. This would be easy with a co-process.

Perderabo,

I can use ksh. Would you mind giving me an idea of how I might accomplish this task
using ksh. Your help would be greatly appreciated.

Right now I am stuck with building a file transfer list based on a numeric ext. that can be in the range of 1-99999. The I am using wget with the no clobber option to execute a loop based on the build list. I still couldn't figure out how to rename randomly generated files, which have been transferred, with a numeric "ext." to ".done"

There has to be an easier way.

There is a problem here. The script is flying blind. You have to tune the sleep command to ensure that the script sleeps until after the file arrives. And the script uses my datecalc script.

#! /usr/bin/ksh

date '+%Y %m %d' | read year month day
today=$(datecalc -j $year $month $day)


#
#       compute start of quarter

((quarter_month=(month-1)/3*3+1))
echo quarter_month = $quarter_month
quarter_start=$(datecalc -j $year $quarter_month 1)
((day_of_quarter=today - quarter_start +1))
echo day_of_quarter = $day_of_quarter


#
#       Set up ftp co-process
HOST=aaaa.bbbb.com
USER=dhdhdh
PASSWD=shshsh

exec 4>&1
ftp -nv >&4 2>&4 |&

print -p open $HOST
print -p user $USER $PASSWD

#
#       Loop on filename
num=0
looping=1
while ((looping)) ; do
        ((num=num+1))
        file=1-${day_of_quarter}-$num
        if [[ ! -f $file ]] ; then
                echo file = $file
                print -p get $file
                sleep 5  #  wait for file to arrive
                if [[ -f $file ]] ; then
                        print -p rename $file ${file}.done
                else
                        looping=0
                fi
        fi
done

print -p bye

wait
exit 0

Perderabo,

Thank you for the script. I am still a novice at shell scripting, so please bear with me. I tried running the script, and I received the following errors. Is this datecalc another script or builtin ksh function ?

./ftptest1.ksh
./ftptest1.ksh[4]: datecalc: not found
./ftptest1.ksh[13]: day_of_quarter=today - quarter_start +1: bad number

You need to get Perderabo's datecalc script here

Perderabo,

Thank you for your help. The script is working perfectly.

One more question, can the datecalc script be used to calculate yesterdays date? Agian, I am dealing with quarter month calculations.

Yes it can. This is covered in the faq section.