Bash Scripting Here Documents
What are here documents?
A here document is basically a special block of code within a script that feeds a command list to an interactive program such as "ftp", "cat", "tr"....
The basic syntax of a here document is as follows:
Here Document Syntax
COMMAND <<BlockOfCodeHERE ... ... ... BlockOfCodeHERE
In the above syntax, the "COMMAND" would be a program. The "BlockOfCodeHERE" marker sets the start and end of the block of code.
ftp here document example
The following example shows how parameters can be passed to a program such as ftp. These parameters are contained within the block "MY_BLOCK":
#!/bin/bash # HOST='ftp.myserver.net' USER='userid' PASSWD='password' FILE='file.txt' ftp -n $HOST <<MY_BLOCK quote USER $USER quote PASS $PASSWD put $FILE quit MY_BLOCK exit 0
tr here document example
tr - Translate Example
#!/bin/bash # tr a-z A-Z << MY_BLOCK one two three four five one two three four five aaa bbb ccc ddd eee fff aaa bbb ccc ddd eee fff MY_BLOCK
Output from above script
john@john-desktop:~/scripts$ ./here1.sh ONE TWO THREE FOUR FIVE ONE TWO THREE FOUR FIVE AAA BBB CCC DDD EEE FFF AAA BBB CCC DDD EEE FFF