"Shell" embedded document here documents

Source: Internet
Author: User

First, here documents (embedded documents)
Here, documents. As a way of redirecting, instructs the shell to start reading the output at the current location of the source file until it encounters a text row that contains only one word. All lines of text that are read in this procedure are used as standard input for a command.
Here-documents Use form:

Command <<[-] Limit_string

If you refer to "limit_string" in double or single quotes or escape with an escape character, the text in Here-document will not be extended, that is, the parameter substitution is disabled . Otherwise, all text in the here-document will be evaluated by regular parameter extensions, command substitution, and expression. In the latter case, the character sequence \<newline> will be ignored and must be escaped with the metacharacters \, $, and ' use \ '.

If you use << instead of <<-, the following limit_string must be at the beginning of the line, otherwise, if the here_document is used inside the function, it will report a syntax error; all the content behind the function will be treated as here_ The content of the document.

If the redirection operator is <<-, all the prefix tab characters in the Msg_body and limit_string rows are ignored (but the spaces are not ignored). This allows the here-documents in the source code to be aligned in an elegant embedding manner.

Second, the application of technology
Here's a look at the need to use here documents in your shell script. Shell script for text file processing is very convenient, so the implementation of this article to complete the work is relatively simple, just a cycle of all files to the process. The problem, however, is that every part of the ①-②,②-③ is actually streaming with cut, Echo, cat, and the output of each process as the next step. It's natural to use a redirection method. It's often awkward to use temporary files to store these intermediate results when you're not familiar with bash before. For example: 00000004.REG-> 00000004.cut-> 00000004.line-> all.txt. In this process, the code is very messy, and sometimes have to write multiple scripts to handle redirection, which makes the problem even worse.
So this time, make a decision to write the code a little bit more concise. Within bash, you can use here to redirect the input and output of the stream in the same script, without having to disassemble into multiple scripts, and omit a large number of intermediate files, making the code very clear and compact.

The general form of documents here is as follows:
0001 Commands <<id
0002 Here Documents
.......     ......
000n id<\n>
In the above syntax description, the commands of line 0001 is any shell command. An ID is a string identifier that marks the beginning of an embedded document (traditionally, the commonly used identifier is EOF, and so on). Any text can be written from 0002 lines, and the content written here will be passed as standard input to commands. The identifier ID is written on the last line, and the code embedded text ends.

Note the following two points when using here documents to write scripts:

On the 000n line, you must have only one ID, that is, the ID must be preceded by a blank space, followed by a return character. Otherwise, there will be an error in shell processing. This should be particularly noted in practice.

When using <<-, any tab character before the input line and the ID on the 000n line will be ignored;

Embedded documents can be written in plain text (for example, the use of usage) or standard shell statements. If it is a shell statement, surround it with an inverted quotation mark ('), and the standard output of the statement is also given to commands as an embedded document. This feature is very useful.

1. Cat Usage
[Exam@exam-server user]$ Cat aa.sh
1 #!/bin/bash
3 cat<<eof # cat > File << EOF, output to new file instead of stdout
4 Now:
5 ' Date '
[Exam@exam-server user]$ SH aa.sh

You can save here_document output to a file or variable

variable=$ (Cat << EOF


Runsover multiple lines.


Echo $variable

This is varible runs over Mutiplelines.

You can use anonymous commands to receive here_document output, which can be used to annotate blocks of code.

: << EOF

${hostname?} ${user?} ${mail?} # If one of the variables is not set, the error message is printed.



37-month 19:23:24 CST 2006

The above is a simple example, but has basically summed up here documents the use of the method. In aa.sh, EOF is the identity ID of an embedded document. Here in line 4 and 5 is the output of a line of standard text "Now" and a command date. Two parts of the output were sent to the cat command to execute.

2. Shell Edit File

The most common method for Shell editing files is the echo string >> file. But what to do if you want to delete a row. Here document is done.

It is not possible to use VI in here document after Jeremiah Test. The alternative is to use the ED command. At the command line, perform the following:

$ Touch Base.txt
$ ed Base.txt
This is line1.
This is line2.
This is line3.
This is line4.

First create a new file Base.txt, and then ed this file, enter a to indicate the last append, entered four lines of text ... represents the end of the input. Wq means save exit.

Let's use the ED command again to manipulate the file in the shell script. As follows.

Ed Base.txt << EOF
3 # To line 3rd
D # Delete the line
I # Bank increase
This is line 3 new. # New Line Content
. # End
Q # Wq means save

The results of the execution are shown below.

$ sh ed_file.sh && cat Base.txt
This is line3.
This is line1.
This is line2.
This is line 3 new.
This is line4.

For the operations and parameters of Ed, you can view Linux Help or search for relevant information.

3. Shell Control Database

Assume that the following operations are performed to access the database.

$ mysql-u Root
Welcome to the MySQL Monitor. Commands End With; or \g.
Your MySQL Connection ID is 1257
Server version:5.1.35-community MySQL Community Server (GPL)

Type ' help, ' or ' \h ' for help. Type ' \c ' to clear the current input statement.

mysql> use MySQL
Reading table information for completion of table and column names
You can turn off the feature to get a quicker startup with-a

Mysql> select * from user;
Mysql> exit

If we were going to use shell scripting, we could write the following script.


Mysql-u Root << EOF
select * from user;

Perform the following

SH mysql_access.sh

You can see the same result.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.