repeating ftp script question

so i got this request to do this:

  •  Script should check for the file ever 15 minutes on the FTP server�if the file is not found, then the whole script exits.  File will only be created one a week at random.
    

i have gotten this far, but am kind of stuck, also sleep command doesnt work in ftp, so looking for the right statement to use (if, then, else, while, etc.) or should it just be done with a cron?, here's what i have so far, until i get stuck:

#!/bin/bash


HOST='66.77.88.99'
USER='jon'
PASSWD='123456789'
FILE='2011-09-20.txt'
flag="N"

clear
ftp -n $HOST <<EOF
quote USER $USER
quote PASS $PASSWD

(this is the part i'm struggling with:)

?

if [[ -f $FILE  ]]; then
get $FILE
bye
EOF
flag='Y'
else
   sleep 900
fi
done

Can you install software on the machine doing the fetching?

If so ncftp makes this pretty ncftpget does most of the work for you and gives a nice exit status:

  1. Success.
  2. Could not connect to remote host.
  3. Could not connect to remote host - timed out.
  4. Transfer failed.
  5. Transfer failed - timed out.
    ...

So you can just run:

    ncftpget -u $USER -p $PASSWD ftp://$FILE
    case $? in
        0) echo "All done"
        3) echo "File not there yet"
        4) echo "Network timeout"
    esac
    

I'd suggest running a script every 15 mins via cron, that checks for the local file and if it hasn't already been fetched it then attempts ncftpget.
It's also worth considering using the ncftpget -DD option to remove the remote file once you have it.

1 Like

ftp allows to direct output from its command, say ls, to the shell.

As example:

ftp -i my.ftp.server << EOF
cd $my/path
ls $my.file "|wc -l > temp.file"
bye
EOF

This piece will place 1 into temp.file if file $my.file exists, otherwise you will get 0

You can make decision in your script based on that 1 or 0

1 Like

thanks all!
:wink: