Deleting local server file from automated FTP script

Hi,

I want to delete a file on the local server, while connected to remote server through FTP.
I am using the below code for this

                        $FTP_CMD -v -n $HOST <<*! >> $LOGFILE 2>&1 
                        user $USER $PASSWORD
                        cd $DIR
                        put $mainfile
                        ls -l
                        !$RM_CMD /path/test/$file2del >> $LOGFILE 2>&1
*!

The FTP logs tell me that the file gets FTPed but the next command is invalid. Please let me know where I am going wrong.

The variables used are :

FTP_CMD=/usr/bin/ftp
RM_CMD=/usr/bin/rm

I doubt ftp understands your RM_CMD... You know ftp is a quite dumb type of shell, that only understand its own internal commands... look at the man pages, there is a delete, del command

But the same command works when I try to connect manually to the FTP server. And I also tried the delete command which is again not working.

I am using

#!/bin/ksh

Do we have any other alternative?

The issue is that RM_CMD is not environmental variable:

$ RM_CMD=/usr/bin/rm
$ echo $RM_CMD
/usr/bin/rm

$ ftp
ftp> !echo $RM_CMD

$ export RM_CMD
$ ftp
ftp> !echo $RM_CMD
/usr/bin/rm

Now the question I would have is why using the variable RM_CMD at all? Is there a problem with just specifying "rm"?

This code is giving the same error

$FTP_CMD -v -n $HOST <<*! >> $LOGFILE 2>&1 
                        user $USER $PASSWORD
                        cd $DIR
                        put $mainfile
                        ls -l

                        !rm /path/test/$file2del >> $LOGFILE 2>&1
*!

Post the error!

Apologies, wasn't paying attention, didn't notice the hereis document ... LS_CMD didn't need to be an environmental variable. But the leading spaces in front of the "!" are causing the problem:

$ ftp << eof
                               !rm foo >> bar 2>&1
eof

?Invalid command

But this works:

$ ftp << eof
!rm foo
eof

If you need the whitespace, try:

sed -e 's/^ *//' << eof | ftp hostname
    ....
eof

This is the error after I execute the script

./scriptname.sh[51]: -s:  not found
rm: cannot remove .. or .

This is the error in logfile

Connected to xxx.xxx.xxx.x.
220 dummy FTP server (Version 1.1.214.4(PHNE_38458) Mon Feb 15 06:03:12 GMT 2010) ready.
Remote system type is UNIX.
Using binary mode to transfer files.
331 Password required for myuser.
230 User myuser logged in.
250 CWD command successful.
200 PORT command successful.
150 Opening BINARY mode data connection for a.xml.
226 Transfer complete.
200 PORT command successful.
150 Opening ASCII mode data connection for /usr/bin/ls.
total 0
-rw-rw----   1 myuser     mqm              0 Nov 11 08:31 a.xml
226 Transfer complete.
?Invalid command
221 Goodbye.

Please post an excerpt of your script...

What's /path/test/$file2del pointing to? Also show your PWD.

Removing the white spaces before

!

is not working.
Here is an excerpt from my script

        cd /path/test/out
        files2send=`ls *.xml`
        ls -l >> $LOGFILE 2>&1
        echo "\n" >> $LOGFILE 2>&1

        for mainfile in $files2send
        do
                file2del=`echo ${enCfile}.ok`
                $TOUCH /path/test/$file2del

                if [ $? -ne 0 ]
                then
                        echo "${TODAY} : Unable to Create FlagFile \
                        /path/test/$file2del" >> $LOGFILE 2>&1

                        continue
                else
                        echo "${TODAY} : Sending File $mainfile via FTP"  >> $LOGFILE 2>&1
                        echo "\n" >> $LOGFILE 2>&1


                        $FTP_CMD -v -n $HOST <<*! >> $LOGFILE 2>&1
                        user $USER $PASSWORD
                        cd $DIR
                        put $mainfile
                        ls -l

!rm /path/test/$file2del  >> $LOGFILE 2>&1 
*!

My current working directory is

/path

/path/test/$file2del is a flagfile

a.xml.ok

which I created in the code.

Reading the man ftp (linux)] manpage:

Since you are not running the ftp interactively, the "!" fails. Instead:

if $FTP_CMD -v -n $HOST << eof >> $LOGFILE 2>&1
user $USER $PASSWORD
cd $DIR
put $mainfile
ls -l
eof
then
  rm /path/test/$file2del
fi

This will delete the file if the ftp was successful.

Be aware that encoding a password in a script is typically considered to be "bad". Please consider using a .netrc file. Or moving to ssh/scp/sftp with keys. If the latter is an option, man rsync (linux) may be of use.