I'm new to shell scripting. I'm trying to delete a file from a remote server using SFTP protocol. Though I'm able to download the file, but I'm unable to delete post download. This is my code:
sftp -oPort=22 $FTP_USER@$FTP_SVR:$TEST_PATH/TEST_*.dat.gpg $LAND
if [ $? -ne 0 ]; then
Error "Cannot Download TEST file from SFTP Server"
fi
sftp -oPort=22 $FTP_USER@$FTP_SVR:$TEST_PATH/n rm TEST_*.dat.gpg
if [ $? -eq 0 ]; then
echo "Removing TEST Files from SFTP Server"
fi
The sftp as you’re using it is similar to ssh command in that it logs you into the remote server, so try and develop your command without any “if” logic, and add it later if necessary. Here’s a version of what you already have without then if logic:
sftp -oPort=22 $FTP_USER@$FTP_SVR; get $TESTPATH/TEST_.dat.gpg $LAND; rm TEST_.dat.gpg; bye
Note that if you can’t get the file, you also shouldn’t be able to delete it, which makes the “if” logic between the two sftp commands not really critical.