sh script to check if certain files exist on ftp server
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
it'd be cool if they didn't exist it could then run a mutt command line saying which ones didn't exist. Or even cooler, would be if they didn't exist, it could pull down a different file from the ftp (Original_`date +%m-%d-%Y`_Final.pdf) then create the png's that were missing (i have the convert commands to do that) then upload the png's.
My only problem is outside of the convert commands to generate the png's, I don't know how to do any of the other stuff.
Thanks in advance for any help or sample scripts you can provide.
The simplest way would be to use curlftpfs, which allows you to use an ftp host as a local directory.
from the man page:
Code:
curlftpfs - mount a ftp host as a local directory.
The program curlftpfs is a tool to mount remote ftp hosts as local directories. It connects to the host FTP server and maps its directory structure to the path directory.
Example from curlftpfs.sourceforge.net:
Code:
Using CurlFtpFS is quite simple. Take a look at this example:
$ mkdir sunet
$ curlftpfs ftp://ftp.sunet.se/ sunet/
$ cd sunet/
$ ls -l
total 0
Dr-xr-xr-x 3 root root 96 Feb 23 2004 bin
dr-xr-xr-x 2 root root 72 Mar 2 2004 dev
dr-xr-xr-x 2 root root 48 Feb 23 2004 etc
dr-xr-xr-x 2 root root 120 Feb 23 2004 lib
-rw-r--r-- 1 root root 622187310 Mar 11 06:13 ls-lR
-rw-r--r-- 1 root root 76389037 Mar 11 06:15 ls-lR.gz
drwxrwxr-x 37 root root 1272 Feb 27 14:17 pub
dr-xr-xr-x 3 root root 72 Feb 23 2004 usr
You can then create an entry in /etc/fstab so the ftp filesystem is mounted on startup.
The FAQ at the curlftpfs explains how to supply a username and password, and how to set up /etc/fstab.
The curlftpfs web page is very helpful.
You can then write a script using 'ls' or '-f' to check if a file exists, and create it as you would normally.
I only have FTP access to this server though unfortunately.
In that case you will have to script ftp.
You will need to set up .netrc.
You can put all the ftp commands you want to run in a HERE doc in the shell.
I suggest for simplicity you assume the file you want does NOT exist then just copy it over.
This should start you off:
Code:
ftp $host <<-HERE
cd remote-dir-name
put filename
bye
HERE
If you really *must* check first then you can run an ftp ls command and check the output with grep
Code:
#!/bin/bash
function checkfile()
{
filename=$1
ftp $host <<-HERE
ls $filename
bye
HERE
}
function create()
{
# create the file in here
}
function sendfile()
{
local=$1
remote=$2
ftp $host <<-HERE
put $local $remote
bye
HERE
}
if checkfile $remotename | grep $remotename
then
# file exists
else
create $localname
sendfile $localname $remotename
fi
That looks cool, even if I don't understand all of it haha. My first question though is, what do you mean by a "HERE doc". do you just mean insert whatever manual FTP commands I would type in for that line or something?
Took me forever but i'm getting back to this now finally. So for stuff with a $ in front of it, how do you set those to what I want? should I just take those out and substitute them for the actual things?
Also, since I'll be looking for two different files, i'm guessing I would have to make two checkfile functions, one for each pattern i'm looking for from my original post?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.