FTP multiple files to different directories

The script below is written to ftp files to different directories on the destination server, but I'm not sure whether I have written the code correctly or not.

Can anyone help me on this?

#!/bin/sh

FILE_NAMES="FileA
FileB
FileC"

SERVER=xxxx
USERID=abcd
PASSWD=xxxxx
FOLDER="/folders/sub1/
/folders/sub2/
/folders/sub3/"

DIRECTORY=1;

(
echo "
open ${SERVER}
user ${USERID} ${PASSWD}
for folder in ${FOLDER}; do
cd ${folder}
COUNT=1;
for filename in ${FILE_NAMES}; do
if [ ${COUNT} -eq ${DIRECTORY} ]; then
put ${filename}
break;
fi
COUNT=`expr $COUNT + 1`
done
DIRECTORY=`expr $DIRECTORY + 1`
done
close
"
) | ftp -i -n

if [ $? -ne 0 ]; then
echo "File Upload Failed"
else
echo "File Upload Completed"
fi

exit 0

Not gonna fly. You cannot echo sh statements into a ftp client. The ftp client knows "open" and "user" , so that makes sense. But you can't type "for" loops into an ftp client. So you can't echo a "for" loop in either and expect it to work. To see my solution to this sort of thing, navigate:

our home page -> Answers to Frequently Asked Questions -> Automate FTP / Scripting FTP Transfers -> For Loops Within Ftp Comands

Thank you Jim.

Can I check whether the ftp was successful using the script you have given as example?

#! /usr/bin/ksh

HOST=remote.host.name
USER=whoever
PASSWD=whatever

exec 4>&1
ftp -nv >&4 2>&4 |&

print -p open $HOST
print -p user $USER $PASSWD
print -p binary
for job2 in 3t 11 12 13 ; do
print -p cd $job2
CFNAME="$job2".chk
print -p put /home/salazar/chk_data/$CFNAME $CFNAME
done
print -p bye
wait
exit 0

Um...who's Jim? The code looks good, but why not try it? That's how I determine if code works or not.

Checking the return code does not work. The ftp clients seem to return 0 if the connection attempt was successful. You could cd to a second local directory and run a second ftp job to retrieve the files you just transferred. Then compare the retrieved files with the original files. This would be expensive if the files are large. You could also use the ftp dir command and parse the output. This is quite a chore but the recursive ftp script linked in that faq article shows how I did it.

Sorry Perderabo, I accidentelly typed that.

Now I have got the concept.

Thank you so much for your explanation.

The code given below is not working when the target (HOST) server is
"Machine hardware: sun4u
OS version: 5.8"

#! /usr/bin/ksh

HOST=remote.host.name
USER=whoever
PASSWD=whatever

exec 4>&1
ftp -nv >&4 2>&4 |&

print -p open $HOST
print -p user $USER $PASSWD
print -p binary
for job2 in 3t 11 12 13 ; do
print -p cd $job2
CFNAME="$job2".chk
print -p put /home/salazar/chk_data/$CFNAME $CFNAME
done
print -p bye
wait
exit 0

I'm getting a message like "Connection Timeout".
Can anyone tell me the reason why it is not ftping?

I tried your script and it worked for me....mostly. Either you have a bug or a very odd directory structure.

After you do
print -p cd $job2
after the first iteration through the loop, you are in the 3t subdirectory. The next time you will be in 3t/11 then in 3t/11/12 and so on.

Maybe you want a
print -p cd ..
or maybe not... who knows?

But this is not a "connection timeout". Can you ftp manually to the host in question? I would expect that to fail too.

The script is executing, but the files are not ftped to the HOST server.

I executed the script in the command line & immediately the script got completed. After few minutes the "Connection Timeout " message is comming.

When I manually ftp the files to the HOST server it is ftped without any problem.

I cant understand the reason.

Please help me on this.

Isn't that a bit like an editor being "Ed" ?

or possibly a Star Trek reference as in.....

abdr600, don't know what to tell you. I tried it and it worked for me. :confused:

Its working now Perderabo. I didnt change anything, but working now. Thanks for your support.

Can you tell me a replacement command for "print" that can be used in sh, as print wont work in sh.

No I can't. This technique is using a co-process. That is not available in other languages. ksh and pdksh are the only shells with co-process support.