21 shell Here Document/String

Here Document Usage

Here Document it can be understood as "embedded documents" "embedded documents" "Now documents", which data or commands to be processed and the code string together , the amount of data suitable for a command to be processed is small, unnecessarily in which case a separate file.

Generally speaking, when embedding multiple rows of data in a short script, Here Document is useful, but embedding a lot of data, or should keep your logic (your code) and your input (your data ) separation, preferably in different files.

usage Explanation
command <<END
    document
END
  1. commandShell is a command, <<ENDa start flag, ENDthe end flag documentis input document (that is, line by line string).
  2. Use Akebayashi command processing document data portion until it encounters until the terminator END (END terminator is not read)
  3. Terminator END must be on a separate line, and must be the top grid write. Terminator may occur in the normal data stream, as long as it is not the top grid to write a separate line, it will not be as complete flag.
  4. Terminator may be defined by the user
  5. Single or double quotes surround the delimiter document command can replace a failed
  6. In <<and ENDincrease between-来消除制表符的影响
  7. Here Document 常用于向用户显示命令或者脚本的用法信息
demand script result

cat command typically read from the file and the contents output to the display,

With Here Document, cat command can read from the keyboard

 cat <<TEST

> test1

>  Test

> Spaces before TEST # terminator, do not take effect

> TEST # top grid write a terminator, not output

test1

 Test

 TEST

<The second layer is a command prompt

Marks the end of the body can also occur TEST, but as long as it is not a separate row,

And not the top grid to write, I will not take effect.

Here Document use in a script file, content and document conversion in uppercase.
  1. #!/bin/bash
  2. # Immediately document in a script file
  3. tr a-z A-Z<<END 
  4. one two three
  5. AbCdefGH
  6. END

ONE TWO THREE

ABCDEFGH

tr command to convert said capital letters lowercase letters

Command substitution

And then to the command by default, document appear in variable or command will be evaluated or run, Shell will first replace them later

They may be surrounded delimiter single or double quotes makes replacing failed Shell

1. By default

  1. name=qpy
  2. cat <<END
  3. > ${name} is a girl
  4. > END

2. single / double quotes the command to replace a failed END

1) single quotes

  1. cat <<'END'
  2. > ${name} is a girl
  3. > END

2) double quotes

  1. cat <<"END"
  2. > ${name} is a girl
  3. > END

Output: qpy is a girl

输出:${name} is a girl

输出:${name} is a girl

Here Document when used in a script file, if the line in the document's first use of tabs,

By default, the first row of tabs is also treated as part of the body, but can also in <<and ENDincreasing between-来消除制表符的影响

 

Tabs are mainly used for code alignment

1. By default

#!/bin/bash

cat <<END

        aaa

        bbb

ccc

END

2. <<and ENDincrease between-来消除制表符的影响

#!/bin/bash

cat <<-END

        aaa

        bbb

ccc

END

Export

  1. aaa
  2. bbb
  3. ccc

 

Export

  1. aaa
  2. bbb
  3. ccc
Here Document most commonly used functions or display usage information to a user command or script

 

Here String Usage

Here String is a variant of the Here Document

usage Explanation
command <<< string
  1. Shell command is the command, string is a general string.
  2. Here String可用于发送较短数据到进程中
  3. 当字符串中有空格时,必须使用单引号或双引号将字符串包围起来
  4. 使用双引号或不适用引号包围字符串时会解析字符串中的变量
  5. 使用单引号包围字符串时不会解析字符串中的变量
  6. 使用引号是,Here String 还可以接收多行字符串作为命令的输入
  7. 与 Here Document 相比,Here String 在发送变量内容(而不是文件)到像 grep 或者 sed 这样的过滤程序时相当方便
需求 脚本 结果
将小写字符串转换为大写字符串

tr a-z A-Z <<< one

ONE
使用双引号,会解析字符串中的变量

tr a-z A-Z <<< "one two three"

var=two

tr a-z A-Z <<<"one $var there"

ONE TWO THREE

ONE TWO THREE

使用单引号,不会解析变量

var=two

tr a-z A-Z <<<'one $var there'

ONE $VAR THERE

使用引号,Here String 可以接收多行字符串作为命令的输入

tr a-z A-Z <<<"one two there > four five six > seven eight"

ONE TWO THERE FOUR FIVE SIX SEVEN EIGHT

Guess you like

Origin www.cnblogs.com/mianbaoshu/p/12069763.html