Deleting a file with LFTP using variables

Svante picture Svante · Mar 19, 2012 · Viewed 16.3k times · Source

I'm trying to delete a file from an FTP server in my shell scrip using LFTP, but for some reason it will not use my variables, and takes them as literals.

The code:

USERNAME="theuser"
PASSWORD="verygoodpassword"
SERVER="example.com"
BACKUPDIR="thebackups"
FILETODELETE="uselessfile.obsolete"

lftp -e 'rm /${BACKUPDIR}/${FILETODELETE}; bye' -u $USERNAME,$PASSWORD $SERVER

What I want it to do is run:

lftp -e 'rm /thebackups/uselessfile.obsolete; bye' -u theuser,verygoodpassword example.com

But instead it runs:

lftp -e 'rm /${BACKUPDIR}/${FILETODELETE}; bye' -u theuser,verygoodpassword example.com

And of cause it can not find the literal file "/${BACKUPDIR}/${FILETODELETE}" on the FTP server and complains thus.

What am I doing wrong???

Cheers!

Answer

Arnaud F. picture Arnaud F. · Mar 19, 2012

That's because you are using simple quote instead of double quotes.

Change this and will work

USERNAME="theuser"
PASSWORD="verygoodpassword"
SERVER="example.com"
BACKUPDIR="thebackups"
FILETODELETE="uselessfile.obsolete"

lftp -e "rm /${BACKUPDIR}/${FILETODELETE}; bye" -u $USERNAME,$PASSWORD $SERVER