EOF basis of usage of shell

A, EOF usage

EOF is ( the END Of File ) abbreviation represents the custom terminator. Since custom, the EOF is not fixed, can freely set the alias, the linux press ctrl-d represents EOF .

EOF usually with cat can output multiple lines of text.

Its usage is as follows:

<< EOF # start

.... # typing

EOF # end

You can also customize, such as customize:

<< ABC # start

....

ABC # end

By cat with the redirection file and capable of generating additional operations before it familiar with several special symbols

< : Input redirection

> : Redirect Output

>> : output redirection , be added , does not overwrite the contents before

<< intermediate a pair of spaced content standard number input from the command line:

Example 1 :

[root@ren5 ~]# cat <<EOF

> hello

> EOF

hello

We know that cat operation object is a file, but the embodiment 1 in the cat 's operation target is not a file, but the user input; then we can understand Example 1 : In the first document file type "in the Hello ", then the cat file output which content.

In other words we can use a file instead of "<< EOF EOF" .

Conversely, if the file operation command is input object, you can also use "<< EOF EOF" to be replaced.

Examples 2 :

[root@ren5 ~]# cat 1.txt

abc

[root @ ren5 ~] # cat << EOF> 1.txt # to the file 1.txt cover the contents of the input (which may be: CAT> << 1.txt the EOF )

> 123

> 456

> 678

> LOL

> EOF

[root@ren5 ~]# cat 1.txt

123

456

678

LOL

" << EOF EOF role" in the process of execution of user-defined command input, which is similar to a temporary file to play the role, but more convenient and flexible than using the file.

Two, CAT << EOF and cat << - EOF difference

Both are acquiring stdin , and EOF at the end of stdin , output stdout .

We use cat << EOF when we're finished, in need of a new line of input EOF end of stdin input. EOF must write top row, with tabs or not the front spaces.

If the redirection operator is << - , then the delimiter ( the EOF tab (beginning) of the row the Tab ) will be removed. This can be solved because the tabs in the script generated by natural indentation .

Example 1 :

[root@ren5 ~]# cat 2.sh

#!/bin/bash

cat <<EOF

Hello, EOF !

EOF

[root@ren5 ~]# sh 2.sh

Hello, EOF !

[root@ren5 ~]# sh 2.sh

2.sh: Line 4: Warning : immediately document in the second two rows are separated end of file ( required `EOF ')

Hello, EOF !

      EOF

If the end of the decomposition symbol EOF before have tabs or spaces, the EOF will not be treated as the end delimiter, it will only continue to be used as stdin to input.

Examples 2 :

[root@ren5 ~]# cat 2.sh

#!/bin/bash

cat <<-EOF

Hello, EOF !

      EOF

[root@ren5 ~]# sh 2.sh

Hello, EOF !

Although the final EOF have multiple tabs and spaces in front, but will still be treated as the end of the delimiter character which indicates stdin end.

Guess you like

Origin www.cnblogs.com/renyz/p/11373525.html