Here documentation in Shell,

Source: Internet
Author: User

Here documentation in Shell,

We know thatlinuxCommand Line operations can all be placed in the file. After the executable permission is granted, the file can be changed toshellScript. But if we talk to some programsInteraction,shellScripts cannot help us much. For example, editing a file and operating a database. Generallyvi, After enteringhjklOfaioAnd how to execute the script?mysqlLogin inputMysql-u user-pAfter the password, followmysqlFor a series of interactions,shellHow?Here DocumentIt can be used.

What is Here Document

Here DocumentYesLinux ShellA specialRedirection MethodIts basic form is as follows:

cmd << delimiter  Here Document Contentdelimiter

It is useddelimiterContent (Here Document ContentPart)cmdAs the input parameter.

For example, entercat << EOF, The system will prompt you to continue the input, enter multiple lines of information and then enterEOFThe 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:
  • EOFIt is just an identifier and can be replaced with any valid character.
  • EnddelimiterIt must be written in the top level. The front cannot contain any characters.
  • EnddelimiterThere cannot be any characters (including spaces) after it)
  • As the startingdelimiterLeading and trailing spaces are ignored.

Here DocumentNot only can be used on the terminalshellFile can also be used, such as the followinghere.shFile

cat << EOF > output.shecho "hello"echo "world"EOF

Usesh here.shRun this script file and you will getoutput.shThe content of this new file is as follows:

echo "hello"echo "world"
Here Document Deformation

delimiterAnd Variables

InHere DocumentNot only can contain common characters, but can also use variables. For examplehere.shChange

cat << EOF > output.shecho "This is output"echo $1EOF

Usesh here.sh HereDocumentRun the script to obtainoutput.shContent

echo "This is output"echo HereDocument

Here$1Expanded as a script ParameterHereDocument

But sometimes I don't want to expand this variable.delimiterBefore and after"For examplehere.shChange

cat << EOF > output.shecho "This is output"echo $1EOF

Obtainedoutput.shContent is

echo "This is output"echo $1

<<Change<<-

Here DocumentAnother usage is'<<'Change'<<-'. Use<<-The only change isHere DocumentContent sectiontab(Tab) will be deleted, this usage is to writeHere DocumentYou can indent the content to facilitate reading the code.

Two applications of Here Document

shellThe most common method for editing files isEcho string> File. But what should I do if I want to delete a row?Here DocumentThat's it. InHere DocumentVi cannot be used in. The alternative is to useedCommand. 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 thenedThis file, enteraIndicates the last append. Four lines of text are entered..End input.wqSave and exit.

Then we useedCommand, inshellIn 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 complicatedshellOther variables in.3Indicates to3Row,dIndicates deletion, and theniIndicates 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 similaredName.

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.

AboutedFor more information, seelinuxHelp 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 useshellYou 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

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.