Need help creating a script to FTP files to a server and then delete the files that were transfered.

I am trying to FTP files to a Windows server through my Linux machine. I have setup the file transfer with no problems but am having problem deleting those files from the Linux box. My current non-working solution is below. Any ideas, anyone?? :wall: Please be gentle, I'm fairly new to this scripting thing.

#Will move audio files from <Linux> to <Windows>
#Sets the current directory to the audio storage area on the server.

#!/bin/bash
 
cd /var/spool/asterisk/monitor
 
!#Start FTP Process
HOST='<host address>'
USER='anonymous'
PASSWD='12345'
 
ftp -i -n $HOST <<auto_move_ftp.log
user ${USER} ${PASSWD}
 
bin
!#Moves q5000 audio files into the admin folder
cd /Admin
mput q5000*
 
!#Moves q5002 and q5003 audio files into the <City> folder
cd /<City>
mput q5002*
mput q5003*
 
!#Moves q5007, 5008 and 5010 audio files into the <Department> folder
cd /<Department>
mput q5007*
mput q5010*
mput q5008*
 
!#Moves q5200 audio files into the <City 2> folder
cd /<City 2>
mput q5200*
 
!#Moves q5001 audio files into the <Crazy> folder
cd /<Crazy>
mput q5001*
bye
 
rm /var/spool/asterisk/monitor/q5000*
 
rm /var/spool/asterisk/monitor/q5002*
 
rm /var/spool/asterisk/monitor/q5003*
 
rm /var/spool/asterisk/monitor/q5007*
 
rm /var/spool/asterisk/monitor/q5008*
 
rm /var/spool/asterisk/monitor/q5010*
 
rm /var/spool/asterisk/monitor/q5200*
 
rm /var/spool/asterisk/monitor/q5001*

"#!/bin/bash" must be the very first line. Not the second or third. If it's not the first line, you may not get the shell you want, or it may not run at all.

What are the lines beginning with "!" for? If you want a comment, the line should begin with "#", not "!".

I think you're confused on what << is for. It doesn't read a file name. It defines text inside the script to feed into something else -- it acts sort of like file redirection in that programs read data on standard input from it, but it's not really a file.

Try this:

cat <<EOF
This is a line of text
EOF

That should cause cat to print "this is a line of text". It's like cat < filename except it's reading data that your script gives it instead of reading from a file that was redirected into it.

You're feeding commands into FTP the same way, but haven't got it quite right. Try this:

ftp <<SOMESTRING
        commands I want to feed into FTP
SOMESTRING

commands I need to do in the shell

Note that the ending "SOMESTRING" is right at the beginning of the line. This isn't optional. If it's not at the beginning of the line, the here document may just never end!

The !'s in the FTP portion were to comment out those lines, # didn't work and generated "invalid command" when it ran.

So to clarify, this goes at the top:

#!/bin/bash
#Will move audio files from <Linux> to <Windows>
#Sets the current directory to the audio storage area on the server.

and for the FTP:

ftp -i -n $HOST <<auto_move
     ftp commands
auto_move
 
shell command <delete file transfered>
#!/bin/bash

cd /var/spool/asterisk/monitor

#Start FTP Process

ftp -i -n ftpserver << EOF
quote user anonymous
quote pass 12345
bin
cd /Admin
mput q5000*
bye
EOF

rm /var/spool/asterisk/monitor/q5000*

Worked Perfectly! Thanks for the help!