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 <
# 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 <
> 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 <
> 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 <
Here Document
Wednesday, March 10, 2010
Posted by Shivam Agrawal at 9:24 AM
Labels: Learning Linux
Subscribe to:
Post Comments (Atom)
Labels
- Games (10)
- Latest News (1)
- Learning Linux (4)
- Linux Tips N Tricks (4)
- Operating System (5)
- Software(Freeware) (1)
- What is Linux ? (1)
Labels
- Games (10)
- Latest News (1)
- Learning Linux (4)
- Linux Tips N Tricks (4)
- Operating System (5)
- Software(Freeware) (1)
- What is Linux ? (1)
0 comments:
Post a Comment