Linux Data Flow redirection

Source: Internet
Author: User

In fact, a beginning around the file descriptor this big pit went, the more digging deeper, what system File table, Memory index node, VFS and so on all of a sudden out, and I have not so much energy to fix all, so can only shield some of the details of the bottom, for the time being a black box, and then dig ...

1, Linux system, all the resources are treated as files, including all kinds of documents on the disk pictures, and even a variety of devices. A file is a basic logical unit of hardware resources for a Linux organization's computer.

2, the process, specifically, we knock all the commands, essentially, is to operate a series of files, and then produce a series of results. For example, the Common LS command, after no parameters, the Operation object is the current directory (directory is also a file-directory file), and then the directory of all the files in the name of the output to the screen (the screen is also a file AH).

3, the result of the operation has two kinds, one is the correct result, the other is the wrong result. Linux refers to the former as the standard output, the file descriptor is 1, the latter is called the standard error output, the corresponding file descriptor is 2. These two types of result information are separate. By default, both types of information are output to the screen, so that you can see it directly, immediately, and then burn when you read it. If we want to save these two kinds of information to facilitate the review later, we need to use the re-guidance.

4, such as my home directory is such a child:

[Email protected]:~$ ls
Desktop program wine-git public template video picture document download music Desktop

By default, the results are printed directly to the screen.

Now I want to save the results to the File1 file, so I can:

[email protected]:~$ ls > file1


LS a bit, you will find that the current directory more than one file file1:

[Email protected]:~$ ls
Desktop File1 program wine-git public template video picture document download music Desktop

Note here: 1) If the current directory does not have a file1 file, then it will be created, and if a file named File1 already exists, the previous content will be overwritten.

2) > Front If there is no number, the default is 1, which is standard output.

Take a look at the contents of File1:

[Email protected]:~$ cat File1
Desktop
File1
Program
Wine-git
Public
Template
Video
Image
Document
Download
Music
Desktop


It is important to note that the FILE1 filename itself is also in the content of file1, what does this mean? Yes, the problem of order.


5. Let's try this:

[Email protected]:~$ ls program/nothing
LS: Unable to access nothing: No file or directory
program/:
google-chrome-stable_current_i386.rpm

Obviously, the parameter of nothing as LS will show an error because there is no such file in the current directory. Now save the two messages in the right and wrong files, and you can do this:

[email protected]:~$ ls program/nothing >right 2>wrong


Check:

[Email protected]:~$ cat Right
program/:
google-chrome-stable_current_i386.rpm

[Email protected]:~$ cat wrong
LS: Unable to access nothing: No file or directory


6. Do I want to keep the correct information and error messages in the same file? Can do this:

[email protected]:~$ ls program/nothing &> all


Check:

[Email protected]:~$ Cat All
LS: Unable to access nothing: No file or directory
program/:
google-chrome-stable_current_i386.rpm


7. There is also a way to save the correct information and error information in the same file:

[email protected]:~$ ls program/nothing > all 2>&1

Explain this usage: first,> All is 1> all, which redirects the standard output to the all file, and then 2> indicates that the standard error is redirected, but where is the redirect? Then &1 is redirected to the file that the file descriptor 1 points to, which is actually similar to a variable for convenience.

Check:

[Email protected]:~$ Cat All
LS: Unable to access nothing: No file or directory
program/:
google-chrome-stable_current_i386.rpm


Obviously the above usage is the same as this:

[email protected]:~$ ls program/nothing 2> all 1>&2


I don't understand at first, why can't it be like this?

[email protected]:~$ ls program/nothing 2> all 1>all

More intuitive, more clear ah, but is wrong ...

[Email protected]:~$ Cat All
program/:
google-chrome-stable_current_i386.rpm
? record

Reason why ... It is said that because the standard output and standard errors are written in the alternate write all file, because there is no rule who first who after, so garbled. and 2> all 1>&2 This approach, is essentially called DUP (2) This system function copy of the previous behavior, in short, is very complex, regardless of the first.


8. What if this happens?

[email protected]:~$ ls program/nothing 1>&2 2> all

Try it yourself, or it's a matter of order.


Linux Data Flow redirection

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.