1752801 Members
5153 Online
108789 Solutions
New Discussion юеВ

FTP script commands

 
SOLVED
Go to solution
Sander Derix
Occasional Advisor

FTP script commands

Hi group,

I've used to be a VB-programmer but I recently switched jobs and now I'm a oracle developer.
I need to build a UNIX ftp script that gets or puts a certain file from/to a certain directory. I've got some sample scripts and I think I can copy/paste enough to get what I need. Problem is that I don't like code in my scripts that I don't fully understand.

Can anybody give me some tips/ links to sites where I can get good information about the mentioned subject?

script so far:
################
## Constants ##
################
FILE=$1
SERVER=$2
USERNAME=$3
PASSWORD=$4
FTP_DIR=$5
#LOCAL=$6
#######################
# Validate Parameters #
#######################
if [ ! "$FILE" ]
then
echo "Must Pass a FILE"
exit 1
fi

if [ ! "$SERVER" ]
then
echo "Must Pass a SERVER name"
exit 1
fi

if [ ! "$FTP_DIR" ]
then
echo "Must Pass a FTP Directory"
exit 1
fi

if [ ! "$LOCAL" ]
then
echo "Must Pass a LOCAL Directory"
exit 1
fi

########################
# LOG #
########################
LOG=/tmp/$1.log

IP_ADDRESS=$2
USERNAME=$3
PASSWORD=$4

########################
#Start Ftp and get file#
########################
ftp -nv ${SERVER} << FTPEOF > /tmp/temp_log.$$
user ${USERNAME} ${PASSWORD}
#Get file
get $1
quit

-------------------
I found (somewhere on the net) what ftp -nv means but what's going on with the < bladibladibla. It probably means that if the end of file is found put something in the log but how what why when?

Regards,

sander
6 REPLIES 6
harry d brown jr
Honored Contributor

Re: FTP script commands


have a look at this thread, as I prefer Clay's method:

http://forums.itrc.hp.com/cm/QuestionAnswer/1,,0x459a8cc5e03fd6118fff0090279cd0f9,00.html

live free or die
harry
Live Free or Die
Tom Maloy
Respected Contributor

Re: FTP script commands

The "<< FTPEOF" is the start of a shell construct called a "here document". All of the lines following that one, up to a line with just "FTPEOF" on it, are included in the "here document". In this case, they are commands that will be fed to the ftp program as input.

The other construct, "> /tmp/temp_log.$$", sends the ftp output into a file named "/tmp/temp_log.$$".

Sounds like you need a good book/tutorial on shell programming. There are a number of shells: sh, csh, ksh, bash, ... I favor ksh, but you should ask your colleagues what they use - then you can always ask them for help, and they will be able to understand your scripts.

Tom
Carpe diem!
Tom Danzig
Honored Contributor
Solution

Re: FTP script commands

As explained previously, it's a shell "here document". I do not, however, see the EOF marker at the end of your post. It should read:

########################
#Start Ftp and get file#
########################
ftp -nv ${SERVER} << FTPEOF > /tmp/temp_log.$$
user ${USERNAME} ${PASSWORD}
#Get file
get $1
quit
FTPEOF


The "FTPEOF" which signifies the end of the here doc commands was missing. Perhaps it's there but just wasn't in you post.
Sander Derix
Occasional Advisor

Re: FTP script commands

Hi there,

thnx for the replies!

Tom you are correct I need a good book. But since programming these (simpel) scripts happens about 1 a year for us I was hoping that there where some good sites in wich this script programming is explained.

Regards,

Sander
Steve Faidley
Valued Contributor

Re: FTP script commands

You don't want the password as a commandline argument.
Anyone can do a ps -ef while it's running and see your password.
Either put it in the file like Sandip and Clay did in the link that was given,
or ask for it when the command is executed.
Ex:
echo "Enter Password: \c"
read password

ftp -nv
open ftp.mysite.com
user $user
pass $password
......
..
If it ain't broke, let me have a look at it.
Mark Ellzey
Valued Contributor

Re: FTP script commands

Sander,

The construct you refer to is commonly called a 'here doc'. What it means is that the text following the << is redirected to stdin, as if you typed it on the keyboard. The FTPEOF is the end marker. Actually, there is an error in your code. It should read as follows:

ftp -nv ${SERVER} << FTPEOF > /tmp/temp_log.$$
user ${USERNAME} ${PASSWORD}
#Get file
get $1
quit
FTPEOF

Note the FTPEOF at the end of the file. This tells the redirection to stop at that point.

Hope this helps,
Mark