FTP and run a loop for array problem

Hello,
I have a problem with my script whereby it does not want to loop.
The function of my script is to FTP into a server and go to each directory/volume in the array 'VOL'. The way the loop is suppose to work, is to go into the first volume, get the files of R(yesterday's date) and save them in another server .. 'REPOSITORY' and then move on to the other volume in the array.
The problem with my script is that it does not loop. It just runs in the first directory then ends.

if [ $# -ne 1 ]
then
        DAISAGO=1
else
        DAISAGO=$1
fi

YESTERDAY=`date -d ${DAISAGO}day+ago +%y%m%d`
REPOSITORY=/home/CDR-backups
cd $REPOSITORY

VOL=(D010AMAB D000AMAA D010AMA9 D000AMA8 D010AMA5 D000AMA6 D010AMA7 D000AMA4 D010AMA3 D000AMA2 D010AMA1 D000AMA0)

ftp -n ##.##.##.## << !EOF
user ****** ******
binary
lcd $REPOSITORY
for VOL in ${VOL[@]}
   do
    cd /$VOL
    pwd
    dir
    mget R${YESTERDAY}*
   done
bye

Below is the output after running my script.

CDR:/home # ./getama.sh
Connected to ##.##.##.##
220 Welcome to Bla Bla Bla
331 Password required.
230 User logged in, proceed
Remote system type is SOS.
200 Command okay.
Local directory now /home/CDR-backups
usage: for format
?Invalid command.
200 Directory changed.
257 Current working directory is "/D010AMAB"
500 Syntax error, command unrecognized.
227 Entering Passive Mode (##,##,##,##,11,186)
150 File status okay; about to open data connection.
DIRP_FILESEG FIXED 2048 ********
DIRP_FILESEG FIXED 2048 ********
DIRP_FILESEG FIXED 2048 ********
226 Request successful. Closing data connection.
?Invalid command.
221 Service closing control connection.

Can someone please help me.

Please and thanks.

ftp is NOT a scripting language. You could create a string (script) using your loop logic from the shell and pass the resulting created script string as input to ftp.

Your version of ftp client might not like a large set of commands coming at it using the mechanism you are using though. Try it.

I'm sorry, I don't quite understand. This is the first time I'm writing a shell script and I've been following some tutorials. Are you saying that I can't run a For Loop while being in an ftp session?

Also ... I'm not familiar with string (script). I tried googling it but I can't come across anything useful. Can you direct me to tutorials? I would really appreciate it.

thanks.

Yes that is what he is saying, you can only run basic FTP commands while in the FTP session.

There are plenty of articles around on passing a script as input to ftp, here is one I found on this forum:
http://www.unix.com/shell-programming-scripting/24522-loops-within-ftp-shell-session.html

Hope this helps :slight_smile: