FTP client won't interpret variables

Issues related to applications and software problems
Post Reply
le_jawa
Posts: 11
Joined: 2012/12/07 19:57:27
Location: Columbus, OH

FTP client won't interpret variables

Post by le_jawa » 2016/07/08 15:16:09

Hello all,

I ran into something that seemed unusual (and frustrating) last night, and wanted to see if anyone could help me understand it, and maybe even show me a better way to work around it. I was writing a script that would (among other things) use the ftp client to create a directory on one of our FTP servers. I set up an FTP script in a loop with a heredoc and passed FTP the username, password and directory names with variables, so the FTP portion of the script looked like this:

Code: Select all

ftp -n ftphost << EOS
    user $ftpuser $ftppass
    mkdir /exisiting_dir/$newdir
    bye
EOS
With every other command-line FTP client I've ever used, that works fine. However, it doesn't work at all on CentOS 6.8; the CentOS ftp client literally passes the string $ftpuser (and the other variable names) instead of the contents of said variables. I tried the quote command, putting quotes around the variable names and a few other ideas to make it work, and nothing did. I finally settled on a combination of .netrc for the user name and password (better idea anyway) and writing the ftp commands out to a temp file, so that BASH expanded the variables where FTP wouldn't. I then had ftp read that file instead of using a heredoc.

For anyone who has encountered the same problem, here's my solution:

Contents of the ~/.netrc file:

Code: Select all

machine ftphost
login mylogin
password mypassword
And the ftp portion of the script itself:

Code: Select all

...
    echo mkdir /existing_dir/$newdir >> /tmp/cmds.ftp
    echo bye >> /tmp/cmds.ftp
    ftp ftphost < /tmp/cmds.ftp
    rm /tmp/cmds.ftp
...
It's a sloppy solution (writing commands to a file, then reading the commands back from the file), but it does work. That said, it shouldn't be necessary. So, all said and done, I have two questions:

Why won't the CentOS 6.8 ftp client handle variables correctly?
What do I need to do to make the FTP client on CentOS 6.8 work with variables?

Thank you,
Jason

forumitu
Posts: 118
Joined: 2014/02/20 14:30:51

Re: FTP client won't interpret variables

Post by forumitu » 2016/07/11 09:56:22

Actually it is not the ftp client which interprets the variables, but the shell script. Most likely your problem is within the script itself. For debugging, if you can afford, just replace the program "ftp" with "cat" to see if it does not echo the content of the variables similarly as expected.

On the command line haven't you used:

Code: Select all

ftp -n ftphost << "EOS"
instead of

Code: Select all

ftp -n ftphost << EOS
accidentally?

le_jawa
Posts: 11
Joined: 2012/12/07 19:57:27
Location: Columbus, OH

Re: [SOLVED]FTP client won't interpret variables

Post by le_jawa » 2016/07/11 14:46:37

Dang, that is exactly what I did (good catch), but not by accident; I was trying to keep everything indented, and the double-quotes let me do it. I know single quotes will force any special characters to be interpreted literally, but I didn't expect double-quotes to have the same effect. I did some digging around after your response and found out I was wrong about that last part. A little more consistency in bash would be nice, but oh well...

Thanks for catching and correcting me on that; it gave me a good chance to go and research things more thoroughly.

-Jason

Post Reply