Operating System - HP-UX
1753330 Members
5298 Online
108792 Solutions
New Discussion юеВ

Pass variable to the ftp script

 
Stephen Ng
Occasional Advisor

Pass variable to the ftp script

I have the following ftp script and I want the mget or get to copy a different no. of files each time.

#vi ftpscript
#/!bin/ksh
ftp $remote_sys <
3 REPLIES 3
S.K. Chan
Honored Contributor

Re: Pass variable to the ftp script

I modified your script to look like this (example) :-

#!/bin/ksh
{ echo user skchan mypassword; echo mget $1; echo quit ; } |ftp -i -n $2

You would then run it like so ..
# for file in $(cat d)
> do
> ./ftpscript $file
> done

The $1 takes in the filename one at a time from the for loop and $2 is the system hostname that you're ftping to. The downside is of course you got to run ftp each time you want to retrieve a file which I suppose is what you wanted because you don't want to use pattern matching to retrieve multiple files in a single session.



Jean-Luc Oudart
Honored Contributor

Re: Pass variable to the ftp script

why not generate a script on the fly :

#!/bin/sh

echo 'ftp $remote_sys<> newscript
echo 'user $remote_user $remote_pass' >>newscript
cat listfile | while read filename
do
echo "get $filename" >>newscript
done
echo "bye" >>newscript
echo "EOP" >>newscript

=================================
newscript is the script to be executed and listfile is the file with the list of file to be trasnferred. This file is dynamic.

don't forget "chmod +x newscript"

Jean-Luc
PS : mind the single and double quote
fiat lux
Rory R Hammond
Trusted Contributor

Re: Pass variable to the ftp script

Below is script I use to get data. Using MGET can some time get to much stuff Trick I use, Is I first get a list of files on the system. parse the list and then get the "stuff" I need. Just a different way to look at the problem.
#!/bin/sh
#
# ftpget.sh
# Purpose: Retrieves files from a remote location
# VIA FTP. This file is usually called from
# a crontab entry.
#
# Written by: Rory Hammond
#
# I modifed this script by shortening it so that I can share the
# concepts The original had a little more error checking and then
# uploaded data into a database

#
# variables need to work
PATH=$PATH:/usr/bin
umask 0
GATEWAYPW="user proxyid proxpasswd"
REMOTEUPW="user userid passwd"
REMOTELOC="ftp.location.coml" #can also be IP Address
PUTDIR="/usr/spool/receive"

#
# Getlist lists ftp into site and list files available
# At this site I was only looking for TXT files hence the grep.
#
getlist()
{
( echo ${GATEWAYPW}
echo ${REMOTEUPW}
echo "ascii"
echo "nlist"
echo "quit"
) | ftp -n ${REMOTELOC} |grep TXT
}

#
# This gets the files gotten from the getlist.
#

getfile()
{
sleep 5
( echo ${GATEWAYPW} # do not need the first two lines if you don't have a
sleep 5 # a gateway user ID and password to get out
echo ${REMOTEUPW}
sleep 5
echo "binary"
echo "get $1 $1"
sleep 5
echo "quit"
) | ftp -n ${REMOTELOC}
}

#
# grep txt will get all file names ending with txt
# it would be faster to put this loop in the getfile
# script macro but I chose not to. After words
# you check the the line count of getlist and compare it
# with the number of files you did receive.
# As a variation you can change the script put files
# and use the getlist to enuser the the ftp worked.
#
for arg in `getlist`
do
cd ${PUTDIR}
getfile ${arg}
echo "`date` $0: --- getfile ---"
echo "$0:Getting ${arg} and putting it at" ${PUTDIR}/${arg}
echo "`date` $0: -----getfile----- transferring ${arg} copy to Mercator"
tranfile ${arg}
done

exit
There are a 100 ways to do things and 97 of them are right