Unix/linux Shell Programming Combat: Using embedded documents here documents
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
Msg_body
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
2
3 Cat <<eof # cat > File << EOF, output to new file instead of stdout
4 Now:
5 ' Date '
6 EOF
[Exam@exam-server user]$ SH aa.sh
Now:
You can save here_document output to a file or variable
variable=$ (Cat << EOF
This Varibale
Runs over multiple lines.
EOF)
Echo $variable
This is varible runs over mutiple lines.
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.
Eof
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
A
This is line1.
This is line2.
This is line3.
This is line4.
.
Wq
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.
#!/bin/sh
Ed Base.txt << EOF
3 # To line 3rd
D # Delete the line
I # Bank increase
This is line 3 new. # New Line Content
. # End
W
Q # Wq means save
Eof
The results of the execution are shown below.
$ sh ed_file.sh && cat Base.txt
60
This is line3.
65
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
Bye
If we were going to use shell scripting, we could write the following script.
#!/bin/sh
Mysql-u Root << EOF
Use MySQL
select * from user;
Exit
Eof
performed as follows.
SH mysql_access.sh
You can see the same result.