Here documentation in Shell
We know that operations on linux Command lines can be put in files. After the executable permission is granted, the file can be converted into a shell script. But if we talk to some programsInteractionBut shell scripts cannot help us much. For example, editing a file and operating a database. Editing a file usually uses vi. after entering the file, what is the hjkl aio operation? How is the script executed? After logging on to mysql and entering the mysql-u user-p password, I will interact with mysql. How can I execute shell? Here Document can be used.
What is Here Document
Here Document is a special feature in Linux Shell.Redirection MethodIts basic form is as follows:
cmd << delimiter Here Document Contentdelimiter
It is used to pass the Content (Here Document Content) between two delimiter to cmd as the input parameter.
For example, if you enter cat <EOF in the terminal, the system will prompt you to continue the input, enter multiple lines of information and then enter EOF. 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 is just an identifier. It can be replaced with any valid character as the end of delimiter must be written in the top level, there cannot be any character before it as the end of delimiter, nor any character (including space) before and after the start of delimiter will be omitted
Here Document can be used not only on the terminal, but also in the shell file, for example, the following here. sh File
cat << EOF > output.shecho "hello"echo "world"EOF
Run the script file by using sh here. sh, and the new file output. sh will be obtained. The content in the file is as follows:
echo "hello"echo "world"
Here Document Deformation
Delimiter and variable
In the content of Here Document, you can not only include common characters, but also use variables in it. For example, you can change the preceding here. sh
cat << EOF > output.shecho "This is output"echo $1EOF
Run the script sh here. sh HereDocument to get the output. sh content.
echo "This is output"echo HereDocument
$1 is expanded as the script parameter HereDocument
But sometimes you don't want to expand this variable. You can add "before and after the start of delimiter. For example, you can change here. sh
cat << EOF > output.shecho "This is output"echo $1EOF
The output. sh is
echo "This is output"echo $1
<Changed to <-
Here Document also uses '<' To Change '<-'. The only change using <-is that the tab (tab) at the front of each line in the content section of the Here Document will be deleted, this method is used to indent the Content Part Of The Here Document for easy reading of the Code.
Two applications of Here Document
The shell edits the file.
The most common method for shell file editing is echo string> file. But what should I do if I want to delete a row? Here Document is done. You cannot use vi in Here Document. The alternative is to use the ed 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
First create a new file base.txt, And Then ed this file, enter a to indicate the last append, and enter four lines of text .. End input. Wq indicates saving and exiting.
Then we use the ed command to operate the file again in the shell script. 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! Compile base.txt and use the variable! ED1_JEREMIAH! The variable here is complicated to distinguish it from other variables in shell. 3 indicates to 3rd rows, d indicates to delete, I indicates to Add rows, and input this is line 3 new. Others are the same as above. Last use! ED1_JEREMIAH! End. That is, two! ED1_JEREMIAH! Each line is similar to the command line input to the ed name for interaction.
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.
You can view the linux help or search for related information about ed operations and parameters.
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 this feature to get a quicker startup with -A mysql> select * from user; mysql> exit Bye
If you want to use a shell script, 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