Here documentation in Shell,
We know thatlinux
Command Line operations can all be placed in the file. After the executable permission is granted, the file can be changed toshell
Script. But if we talk to some programsInteraction,shell
Scripts cannot help us much. For example, editing a file and operating a database. Generallyvi
, After enteringhjkl
Ofaio
And how to execute the script?mysql
Login inputMysql-u user-p
After the password, followmysql
For a series of interactions,shell
How?Here Document
It can be used.
What is Here Document
Here Document
YesLinux Shell
A specialRedirection MethodIts basic form is as follows:
cmd << delimiter Here Document Contentdelimiter
It is useddelimiter
Content (Here Document Content
Part)cmd
As the input parameter.
For example, entercat << EOF
, The system will prompt you to continue the input, enter multiple lines of information and then enterEOF
The information entered in the middle will be displayed on the screen. As follows:
fish@mangos:~$ cat << EOF> First Line> Second Line> Third Line EOF> EOFFirst LineSecond LineThird Line EOF
Note:>
This symbol is the identifier of the prompt input information generated by the terminal.
Note the following points:
EOF
It is just an identifier and can be replaced with any valid character.
- End
delimiter
It must be written in the top level. The front cannot contain any characters.
- End
delimiter
There cannot be any characters (including spaces) after it)
- As the starting
delimiter
Leading and trailing spaces are ignored.
Here Document
Not only can be used on the terminalshell
File can also be used, such as the followinghere.sh
File
cat << EOF > output.shecho "hello"echo "world"EOF
Usesh here.sh
Run this script file and you will getoutput.sh
The content of this new file is as follows:
echo "hello"echo "world"
Here Document Deformation
delimiter
And Variables
InHere Document
Not only can contain common characters, but can also use variables. For examplehere.sh
Change
cat << EOF > output.shecho "This is output"echo $1EOF
Usesh here.sh HereDocument
Run the script to obtainoutput.sh
Content
echo "This is output"echo HereDocument
Here$1
Expanded as a script ParameterHereDocument
But sometimes I don't want to expand this variable.delimiter
Before and after"
For examplehere.sh
Change
cat << EOF > output.shecho "This is output"echo $1EOF
Obtainedoutput.sh
Content is
echo "This is output"echo $1
<<
Change<<-
Here Document
Another usage is'<<'
Change'<<-'
. Use<<-
The only change isHere Document
Content sectiontab
(Tab) will be deleted, this usage is to writeHere Document
You can indent the content to facilitate reading the code.
Two applications of Here Document
shell
The most common method for editing files isEcho string> File
. But what should I do if I want to delete a row?Here Document
That's it. InHere Document
Vi cannot be used in. The alternative is to useed
Command. Run the following command on the command line:
$ touch base.txt $ ed base.txt a this is line1. this is line2. this is line3. this is line4. . wq
Create a new file firstbase.txt
, And thened
This file, entera
Indicates the last append. Four lines of text are entered..
End input.wq
Save and exit.
Then we useed
Command, inshell
In the script, the file is operated again. As follows.
#!/bin/sh ed base.txt << !ED1_JEREMIAH! 3 d i this is line 3 new. . w q !ED1_JEREMIAH!
Explanation:ed base.txt << !ED1_JEREMIAH!
Indicates editingbase.txt
, Using variables!ED1_JEREMIAH!
Mark, the variable here is complicatedshell
Other variables in.3
Indicates to3
Row,d
Indicates deletion, and theni
Indicates that this row is added. Enterthis is line 3 new
. Others are the same as above. Last use!ED1_JEREMIAH!
End. That is to say, two!ED1_JEREMIAH!
Each line is similared
Name.
The execution result is as follows.
$ 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.
Abouted
For more information, seelinux
Help or search for related materials.
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 this feature to get a quicker startup with -A mysql> select * from user; mysql> exit Bye
If you want to useshell
You can write the following script.
#!/bin/sh mysql -u root << !ED2_JEREMIAH! use mysql select * from user; exit !ED2_JEREMIAH!
Run the following command.
sh mysql_access.sh