First this an example using Here document:
#
# Here document
#
ftp -vn localhost << !
user $USER $PASSWD
pwd
quit
!
Another way using quote Ftp command:
ftp -n $HOST << !
quote USER $USER
quote PASS $PASSWD
pwd
quit
!
The previous ways are good as long as we don't need to do some logic inside the Ftp connection, like for example looping or conditional statements.
Instead, to able to do so, we have to use another method using "co-process" technique in the Korn shell (ksh) or of course Perl.
#
# Using co-process
# The "|&" turns the process into a co-process that allows subsequent
# "print -p" statements to send lines to the co-process standard-in and
# "read -p" to read from its standard-out.
#
exec 4>&1
ftp -nv >&4 2>&4 |&
print -p open localhost
print -p user ericsson ericsson
print -p pwd
for i in 1 2 3 ; do
print -p pwd
done
print -p bye
wait
References:
Inlumine Consulting
Unix forums