Shell redirection and file descriptors

Source: Internet
Author: User

Explain

Redirect: Is an action, an Operation action action

File descriptors: Nouns, another representation of a file, Linux everything is file.


"Base redirection form"

< Input redirect Usage Rating: * *
<< Here-document Input redirect

Use star: ** *

> Output redirection Usage Rating: * * * *
>> Additional Usage Rating: * * * *



First, < usage: Cat as a representative

Cat features: Concatenate FILE (s), or standard input, to standard output.


1 connection file, output to standard output (i.e. print to screen)

2 standard input to standard output (i.e. print to screen)


Cat/etc/issue equals Cat </etc/issue


But the cat's first function is used on the left, and the second function of cat is used on the right.




B. Usage of <<: start with Word, end with delimiter (same as word), and the contents of the document in the middle. Cat as a representative


<<[-]word

Here-document

Delimiter



[[email protected] std]# cat << EOF

> I am Linux

> Yes.

> EOF

I am Linux

Yes.

[[email protected] STD]


Note:<< document Here-document as input, Cat is outputting the here-document to the screen.


Common: in shell scripts



Third, the use of >:

1 emptying a file

> Youfile

2 Empty the contents of the file, write the new content

echo "You content" > file.txt



Iv. >> Append usage append content to file


echo "test message" >>/var/log/message



"Base File Descriptor form"


Digital Code Significance Use
0 Standard input STDIN *****
1 Standard output STDOUT *****
2 Error Output STDERR *****



"Combination of file descriptors and redirects"

Default

< equals 0< standard input

> equals 1> Callout Output redirect


>> equals 1>> standard output append


Other

2> can only 2> error output redirection

2>> only >> error output append

&> equals > file 2>&1 standard output and error output redirection, only once files are opened.

&>> equals >> file 2>&1 standard output and error output append, only once files are opened.



Here's one thing to understand,

Why is

&>> equals >> file 2>&1 instead of >> file 2>>&1 This will cause an error.

Because the file is opened, it is appended, so 2>&1 can indicate that the error is output to that file (the file is already open and the cursor is at the end of the line).


The difference between redirection and appending is whether the file is emptied when the file is first opened, and the beginning of the cursor is defined. Writes from the first character of the first line.

Mode + Write = result



Analyze a few echoes

echo "abc" > Test.txt

echo "Error" >> text.txt

echo "abc" >&2 #这一行表示的是错误输出 equals echo "abc" 1>&2, preceded by a default shorthand



"Custom file Descriptor"

Create your own redirects in the shell without limiting the 0 1 2 to this three default file descriptor. In fact, there can be up to 9 open file descriptors in the shell. The other 6 were 3-8. And can be redirected as input and output.

You can assign any one of these file descriptors to a file.


Example 1: Creating an output file descriptor

[email protected] std]# cat test.sh

#/bin/bashexec 3> File3.txtecho "first line 1" echo "first line 3" >&3


Note: Exec performs redirection of file descriptor 3 to the File3.txt file.

Get the results.

[Email protected] std]# sh test.sh

First Line 1

[email protected] std]# cat File3.txt

First Line 3


Example 2: File in script as redirect input

#/bin/bashexec 0</etc/issuewhile Read line; Do Echo $linedone


Explanation: The file/etc/issue as input to the 0 file descriptor.

Read line reads from the file descriptor 0 (keyboard) by default, and exec already defines the input from the/etc/issue file, so read reads the/etc/issue file, assigning the contents of a row to lines the variable. While in the loop reads the file in a downward line .....


Equivalent to

#/bin/bashcat/etc/issue |while read line; Do Echo $linedone



Example 3: Closing Open file descriptors

#/bin/bashexec 3>>/etc/issueecho "This is the last file" >&3exec 3>&-echo "test Output" >&3


Note: Once you close the file descriptor, you cannot write any data to it in the script, or the shell generates an error message.

[Email protected] std]# sh test.sh

Test.sh:line 7:3: Bad file descriptor




This

You're going to have a lot to do with the EXEC command. What is the difference between executing a script with source and SH?

Read the next article.



This article is from the "learning communication, the first job ops" blog, please be sure to keep this source http://cuidehua.blog.51cto.com/5449828/1832007

Shell redirection and file descriptors

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.