★Welcome to Linux For You™.... a page dedicated to open source technology★

Here Document

Wednesday, March 10, 2010

A here document is a special-purpose code block. It uses a form of I/O redirection to feed a command list to an interactive program or a command, such as ftp, cat, or the ex text editor.
COMMAND <<< designates the limit string. This has the effect of redirecting the output of a file into the stdin of the program or command. It is similar to interactive-program < command-file, where command-file contains command #1 command #2 ... The here document alternative looks like this: #!/bin/bash interactive-program <<<.

# Bram Moolenaar points out that this may not work with 'vim'
#+ because of possible problems with terminal interaction.

Exit
The above script could just as effectively have been implemented with ex, rather than vi. Here documents containing a list of ex commands are common enough to form their own category, known as ex scripts.
#!/bin/bash
# Replace all instances of "Smith" with "Jones"
#+ in files with a ".txt" filename suffix.

ORIGINAL=Smith
REPLACEMENT=Jones

for word in $(fgrep -l $ORIGINAL *.txt)
do
# -------------------------------------
ex $word << $Newfile <<<-LimitString) suppresses leading tabs (but not spaces) in the output. This may be useful in making a script more readable. For those tasks too complex for a here document, consider using the expect scripting language, which was specifically designed for feeding input into interactive programs. Here documents To create a here document use the following syntax: command << This is a test.
> Apple juice.
> 100% fruit juice and no added sugar, colour or preservative.
> EOF
Sample outputs:
16
The <<, reads the shell input typed after the wc command at the PS2 prompts, >) up to a line which is identical to word EOF.
Here document
A here document (also called a here-document, a heredoc, a hereis, a here-string or a here-script) is a way of specifying a string literal in command line shells including all the Unix shells (sh, csh, ksh, Bash and zsh) and in programming or scripting languages such as Perl, PHP, Python and Ruby. It preserves the line breaks and other whitespace (including indentation) in the text. Some languages allow variable substitution and command substitution inside the string.
The general syntax is << followed by a delimiting identifier, followed, starting on the next line, by the text to be quoted, and then closed by the same identifier on its own line. Under the Unix shells, here documents are generally used as a way of providing input to commands Specific implementations The following provides an overview of specific implementations in different programming languages and environments. Most of these are identical or substantially similar to the general syntax specified above, although some environments provide similar functionality but with different conventions and under different names. Command line shells Unix-Shells In the following example, text is passed to the tr command using a here document. $ tr a-z A-Z < one two three
> uno dos tres
> END_TEXT
ONE TWO THREE
UNO DOS TRES
END_TEXT was used as the delimiting identifier. It specified the start and end of the here document. ONE TWO THREE and UNO DOS TRES are outputs from tr after execution.
By default variables and also commands in backticks are evaluated:
$ cat << EOF > Working dir $PWD
> EOF
Working dir /home/user
This can be disabled by quoting any part of the label. For example by setting it in single or double quotes:
$ cat << "EOF" > Working dir $PWD
> EOF
Working dir $PWD

A here document is a form of quoting that allows shell variables to be substituted. It's a special form of redirection that starts with <<<\WORD, but not on the ending line. To have the same effect the C shell expects the \ in front of WORD at both locations.

0 comments: