Thursday, November 1, 2007

Using "Here Documents" To Automate FTP Processes.

Hey again,

A great tool to use when you want to do a little automation in your scripts is a little thing commonly referred to as a "Here Document." I still don't know why, but it hasn't ever mattered ;)

Of course, the "Here Document" can't automate everything (SSH, Telnet or basically anything that requires an interactive terminal login -- There's an excellent tool for that called Expect - but that's for another day)

The most basic use of a "Here Document" is to help automate FTP'ing of files.

The following scriptlet illustrates this as such:

Creative Commons License

This work is licensed under a
Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License


# 2007 - Mike Golvach -
# Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License


# Now let's ftp that file to our host

# Begin "Here Document"
/usr/bin/ftp <<- END
user bjohnson
cd uploads
put $file

# End of "Here Document"


Notice that the structure of the "Here Document" is fairly simple You have the program you want to invoke on the first line.

That's followed by either <<- KEYWORD or << KEYWORD. The main difference is that the <<- Form will strip leading tabs (not spaces, though) from the lines between the beginning and ending KEYWORD. This option basically arose so that users could write "Here Document"s and make them more easily readable.

The KEYWORD is also important. The only real things that are important about it are that it be the same at the beginning and end. The KEYWORD is a placeholder, telling your "Here Document" what to look for so it knows when it's done :) Another, less conspicuous thing is that it shouldn't be the name of any other variable in the script. General usage dictates all uppercase and, more often than not, the keyword EOF. Ultimately, it's up to you.

Downsides to doing FTP this way are that you have to write the password in plain-text (BJOHNSON above) and, even though this is more of a downside to "Here Document"s altogether, you cannot comment within the KEYWORD brackets.

You can do a lot more with here documents. Pretty much any command you can pipe input to can be used. Have fun trying it out!

, Mike