We know that the usual linux
command line operation can be placed in the file, given the executable permission, you can change the file into a shell
script. But if we interact with some programs, the shell
script doesn't help us too much. such as editing a file and manipulating the database. Edit a file general use vi
, after the hjkl
aio
operation of what, script how to execute? mysql
Login to enter mysql -u 用户 -p
a password, followed mysql
by a string of interaction, shell
how to execute? Here Document
can be useful.
What is here Document
Here Document
is Linux Shell
a special redirection method in which the basic form is as follows
cmd << delimiter Contentdelimiter
Its role is to delimiter
pass the content (part) between the two elements Here Document Content
cmd
as input parameters.
For example, in the terminal input cat << EOF
, the system will be prompted to continue to enter, enter multiple lines of information and input EOF
, the middle input information will be displayed on the screen. As follows:
FirstLineLineLine EOF> EOFFirstLineLineLine EOF
Note: >
This symbol is the identifier for the prompt input information generated by the terminal
Here are some points to note
EOF
It's just a sign, it can be replaced with any legal character.
- As the end
delimiter
must be shelf write, the front cannot have any characters
delimiter
you cannot have any characters (including spaces) after the end.
delimiter
the spaces before and after the start are omitted.
Here Document
Not only can be used on the terminal, shell
but also can be used in the file, such as the following here.sh
file
cat << EOF > output.shecho"hello"echo"world"EOF
Using sh here.sh
run this script file, will get output.sh
this new file, inside the content as follows
echo"hello"echo"world"
Variants of Here Document
delimiter
and variable
In Here Document
the content, not only can include ordinary characters, but also can be used in the variables, such as the above to here.sh
change the
cat << EOF > output.shecho"This is output"echo$1EOF
Using sh here.sh HereDocument
the Run script output.sh
to get the content
echo"This is output"echo HereDocument
$1
is expanded here to be the parameter of the scriptHereDocument
But sometimes you do not want to expand this variable, you can do it by delimiter
adding it before or after the start "
, for example, change the above to here.sh
cat << EOF > output.shecho"This is output"echo$1EOF
Get the output.sh
content for
echo"This is output"echo$1
<<
Into<<-
Here Document
There is one more usage that will ‘<<‘
become ‘<<-‘
. The <<-
only change that is used is that Here Document
the content portion of the front of each line tab
(tab) will be deleted, this usage is for writing Here Document
when you can indent the content part, easy to read the code.
Two apps for here document
- Shell to edit the file.
shell
The most common way to edit a file is echo 字符串 >> 文件
. But what if I want to delete a row? Here Document
it's done. It Here Document
is not possible to use VI in. The workaround is to use the ed
command. At the command line, do the following:
basebasethisisthisisthisisthisis line4. . wq
First create a new file base.txt
, then ed
this file, input a
represents the last append, entered four lines of text. .
represents the end input. wq
indicates that the save exits.
Then we'll use the ed
command again to manipulate the file in the shell
script. As follows.
#!/bin/sh 33 new. . w q !ED1_JEREMIAH!
Explanation: For ed base.txt << !ED1_JEREMIAH!
editing base.txt
, tag with variables !ED1_JEREMIAH!
, the variables here are complex in order to shell
differentiate from the other variables in. Represents 3
to the 3
line, d
means delete, and then i
represents the bank increment, input this is line 3 new
. The others are the same as above. Finally !ED1_JEREMIAH!
end with. That !ED1_JEREMIAH!
is, each line between two is similar to the command line input into the ed
name, to interact.
The results of the execution are as follows.
base.txt 60thisis65thisisthisisthisis3newthisis line4.
For ed
the operation and parameters, you can view linux
Help or go to search for relevant information.
- Shell Control Database
Assume that you perform the following operations to access the database.
$ mysql-u Root Welcome to theMySQL Monitor. Commands End with; or \g. Your MySQL Connection ID is1257Serverversion:5.1. the-community MySQL Community Server (GPL) Type' help; ' or ' \h ' forHelp. Type' \c ' to Clear theCurrent input statement. mysql> use MySQL Reading table information forCompletion ofTable andColumn names can turn off this feature to Get aQuicker startup with-A mysql> SELECT * fromUser Mysql> Exit Bye
If we are going to use shell
a script to access it, we can write the following script.
#!/bin/sh exit !ED2_JEREMIAH!
Do the following.
sh mysql_access.sh
A detailed here document in the Shell