A detailed here document in the Shell

Source: Internet
Author: User
Tags shebang

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? mysqlLogin to enter mysql -u 用户 -p a password, followed mysql by a string of interaction, shell how to execute? Here Documentcan be useful.

What is here Document

Here Documentis 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
    • EOFIt'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
    • delimiteryou cannot have any characters (including spaces) after the end.
    • delimiterthe spaces before and after the start are omitted.

Here DocumentNot 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

delimiterand 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

$1is 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 DocumentThere 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
    1. Shell to edit the file.

shellThe most common way to edit a file is echo 字符串 >> 文件 . But what if I want to delete a row? Here Documentit'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. wqindicates 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.

    1. 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

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.