sftp script - problem removing files remotely


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sftp script - problem removing files remotely
# 1  
Old 10-18-2011
sftp script - problem removing files remotely

Hello all,

After searching through the similar topics and not finding a working solution, I decided to join and post my question (and maybe kill time and help other users).

Essentially I am trying to get a file from the sftp and then delete it after the file is pulled. all the sftp commands work except for the the "rm" command in the script. I get no errors running the script either.

any ideas or suggestions would greatly be appreciated!!


Code:
ftpFile()
{
        #ftp block starting - all will be written to log file

        cd $dataDir
        echo "---------------------------------------------------"
        echo "              Begin FTP Parameters  "
        echo "---------------------------------------------------"
        echo "Email Sent To:        $Email1 "
        echo "Destination Machine:  $eft"
        echo "User ID:              $user"
        echo "Destination File:     ${dataDir}/${sourceFile}"
        echo "---------------------------------------------------"
        echo "                End FTP Parameters  "
        echo "---------------------------------------------------"
        echo "                Begin FTP Session  "
        echo "---------------------------------------------------"
                echo "                                                   "
                echo "---------------------------------------------------"
        echo "        Grabbing $sourceFile from the ftp...  "
        echo "---------------------------------------------------"

                   sftp $user@$eft:$sourcePath$sourceFile

            echo "                                                   "
                echo "---------------------------------------------------"
        echo "       ...Deleting $sourceFile from the ftp  "
        echo "---------------------------------------------------"

                        rm -f $sourceFile

                echo "---------------------------------------------------"
        echo "                End FTP Session  "
        echo "---------------------------------------------------"


        echo which $JAVA_HOME

        # FTP block ends

       # Did we get a file?
        if [ ! -f ${dataDir}/${sourceFile} ]
        then
                mailError "Could not download ${sourceFile}."
        fi
}

# 2  
Old 10-18-2011
I do not how your code could work from the snippet.
Code:
sftp remotnode <<EOF
   get $filename  /path/to/local/$filename
EOF
if [ -r  /path/to/local/$filename ] ; then
 sftp remotnode <<EOF
    rm $filename 
EOF

fi

# 3  
Old 10-19-2011
Can I suggest using the -b batchfile option of sftp. Advantage is that if the get command fails sftp stops processing the batchfile so it won't remove your source file.

Code:
echo "get $filename /path/to/local/$filename" > /tmp/sftp_batch.$$
echo "rm $filename" >> /tmp/sftp_batch.$$
sftp -b /tmp/sftp_batch.$$ user@host 
rm /tmp/sftp_batch.$$

This User Gave Thanks to Chubler_XL For This Post:
# 4  
Old 10-20-2011
Thanks for the replies!
I am still not able to get it to work, I think it may have to do with my formatting of the batch file.

this is what I use for the batch file:

I added a .bat file extension to it so it is called "sftp.bat" and it located in bin directory with all the script. and of course, both the script and bat have chmod 755

Code:
#!/bin/sh

get testfile.csv .
rm testfile.csv
quit

and this is what I use in the actual script.

most of the commands (like directory change) are done here.
Code:
ftpFile()
{
        #ftp block starting - all will be written to log file

        cd $dataDir
        echo "---------------------------------------------------"
        echo "              Begin FTP Parameters  "
        echo "---------------------------------------------------"
        echo "Email Sent To:        $Email1 "
        echo "Destination Machine:  $eft"
        echo "User ID:              $user"
        echo "Destination File:     ${dataDir}/${sourceFile}"
        echo "---------------------------------------------------"
        echo "                End FTP Parameters  "
        echo "---------------------------------------------------"
        echo "                Begin FTP Session  "
        echo "---------------------------------------------------"
                echo "                                                   "
                echo "---------------------------------------------------"
        echo "        Grabbing $sourceFile from the ftp...  "
        echo "---------------------------------------------------"
                  sftp -b sftp.bat $user@$eft

            echo "                                                   "
                echo "---------------------------------------------------"
        echo "       ...Deleting $sourceFile from the ftp  "
        echo "---------------------------------------------------"

                               

                echo "---------------------------------------------------"
        echo "                End FTP Session  "
        echo "---------------------------------------------------"


        echo which $JAVA_HOME

         #FTP block ends

        #Did we get a file?
        if [ ! -f ${dataDir}/${sourceFile} ]
        then
                mailError "Could not download ${sourceFile}."
        fi
}


Thanks for the help again!

---------- Post updated at 11:33 AM ---------- Previous update was at 10:13 AM ----------

Ok looks like I got it working with out using a batch file,

I pretty much started a second ftp session after i grabbed the file to delete the file:

Code:
ftpFile()
{
        #ftp block starting - all will be written to log file

        cd $dataDir
        echo "---------------------------------------------------"
        echo "              Begin FTP Parameters  "
        echo "---------------------------------------------------"
        echo "Email Sent To:        $Email1 "
        echo "Destination Machine:  $eft"
        echo "User ID:              $user"
        echo "Destination File:     ${dataDir}/${sourceFile}"
        echo "---------------------------------------------------"
        echo "                End FTP Parameters  "
        echo "---------------------------------------------------"
        echo "                Begin FTP Session  "
        echo "---------------------------------------------------"
        echo "                                                   "
        echo "---------------------------------------------------"
        echo "        Grabbing $sourceFile from the ftp...  "
        echo "---------------------------------------------------"


                          sftp $user@$eft:$sourcePath$sourceFile

        echo "                                                   "
        echo "---------------------------------------------------"
        echo "       ...Deleting $sourceFile from the ftp  "
        echo "---------------------------------------------------"
                sftp  $user@$eft <<END_SCRIPT
                          lcd $dataDir
                          cd $sourcePath
                          rm $sourceFile
                          qui

END_SCRIPT


        echo "---------------------------------------------------"
        echo "                End FTP Session  "
        echo "---------------------------------------------------"


        echo which $JAVA_HOME

         #FTP block ends

        #Did we get a file?
        if [ ! -f ${dataDir}/${sourceFile} ]
        then
                mailError "Could not download ${sourceFile}."
        fi

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Find and removing the old files and zipping the files using shell script

Hi, I am trying to removing the old files which were older than 10 days and same g zipping the files using the shell script. script was return as follows. find /jboss7_homes/JBOSS7/SKYLIV??/SKYLIV??_CRM/jboss-eap-7.0/standalone/log -mtime +10 -type f | xargs rm -f find /cer_skyliv??/log... (6 Replies)
Discussion started by: venkat918
6 Replies

2. Shell Programming and Scripting

Bash - sftp simple script problem

Hello, when running the scripts below I am not getting message bb2. Can you please help? #!/bin/bash TLOG=/tmp/bb/amatest.log FTPRESULTS=/tmp/bb/amlist export TLOG FTPRESULTS >$TLOG >$FTPRESULTS echo bb1 sftp -oPort=2222 XXXXXXXXXXXXX@sftp.userssedi.com <<EOF cd... (5 Replies)
Discussion started by: biljana
5 Replies

3. Shell Programming and Scripting

Renaming multiple files in sftp server in a get files script

Hi, In sftp script to get files, I have to rename all the files which I am picking. Rename command does not work here. Is there any way to do this? I am using #!/bin/ksh For eg: sftp user@host <<EOF cd /path get *.txt rename *.txt *.txt.done ... (7 Replies)
Discussion started by: jhilmil
7 Replies

4. Shell Programming and Scripting

Script to SFTP files then delete them.

Hi, I want to create a script.sh over my local server doing the following: 1- There are files are creating over a directory over my local server "DIR_1". 2- I need to SFTP transfer these files to another Host "HOST_1" by "USER_1"/ "PASS_1". 3- Then Delete the transferred files from my Local... (2 Replies)
Discussion started by: eng_galileo
2 Replies

5. Shell Programming and Scripting

Problem removing $ using sed in script

I am trying to use the sed command to remove any character that is passed as a parameter to the script. I may want to replace any character in the file (-, =, $, space). I'm running into an issue when passing the $. This is a typical row in the file. "000000B553","--INTERNAL USE... (6 Replies)
Discussion started by: nadneb
6 Replies

6. HP-UX

Problem with sftp for accent character files

Hi All, Following are the problems i am facing.Resolution for these would be highly commendable: a)I am trying to transfer files from my local C: directory to unix server using sftp. The problem is coming when files with accent characters are picked for transfer.These files are not... (6 Replies)
Discussion started by: destinykrishan
6 Replies

7. UNIX for Dummies Questions & Answers

Problem automating sFTP transfer using script in cron

Hi Newbie here I am having problems with automating sFTP transfers. Just to save time - SCP is not an option as sFTP is stipulated by controllers of far end server. Ineed to automate sFTP transfer of a single file, once a day to a remote server to which i have no control over. I am using:... (6 Replies)
Discussion started by: robbien
6 Replies

8. Shell Programming and Scripting

Problem with SFTP script

Hi All, I can log in properly with my sftp handshake with the script, but can not get it to execute the lines of code for get commands within the script. Is there anything special with SFTP we need to do to make this work. I have went and looked on the message board but did not find anything... (3 Replies)
Discussion started by: wsiefkas
3 Replies

9. Shell Programming and Scripting

problem in making sftp script

Dear all I am bit new to shell scripting . I am implemented autossh between two sun solaris machines , so that when I use sftp it will not ask for the password. Now I need to make shell script in which I have to transfer files from one server to another server automatically through root... (8 Replies)
Discussion started by: girish.batra
8 Replies

10. UNIX for Advanced & Expert Users

weird problem with removing files

I have a few files on my system named: -rw------- -rw-r----- -rw-rw--w- -rwxrw-r-x (Yes, it's really the name of the file, not the access permissions, they're 0 bytes large and all created at the same date/time). I've no idea how they got there but I don't seem to be able to delete them... (2 Replies)
Discussion started by: rein
2 Replies
Login or Register to Ask a Question