The shell output of the garbage bin--/dev/null

Source: Internet
Author: User

Last night spent a whole night answering a question about the redirect output to/dev/null in the shell. Decisive tonight also synchronous hair here, anyway no one see ~ ~ ~



The following comes from a heavy Linux use patient's unsolicited answer.

Answer the questions of the main question in simple language first:

What does 2>/dev/null mean in a shell program?

Answer: "2>/dev/null" means ignoring the error message.

What does title 2> mean? Say Error output delete?

A: "2>" represents the redirect Operation error message. Only these two characters do not remove the error output.

If I have the correct output and assign a value to I,i, will I get the correct value?

Answer: I will get the correct value.

The three answers above are those that I think are relatively friendly and easy to understand.


---------------------------------------I'm the one who answered the split line-------------------------------------


Next, let's take a look at the knowledge points that this command operation involves (knocking on the blackboard ....) In fact, the command described in the main question involves three parts of the content, such as:

<img src=\ ' #\ ' "//pic1.zhimg.com/v2-32b909c43d75fa9c1f7c9286fdbffdc4_b.png& quot data-rawwidth="247" data-rawheight="175" class="content_image" width="247">650) this.width=650; "Src=" https:// Pic1.zhimg.com/v2-32b909c43d75fa9c1f7c9286fdbffdc4_b.png "class=" content_image lazy "width=" 247 "style=" width : 247px;height:175px; "alt=" V2-32b909c43d75fa9c1f7c9286fdbffdc4_b.pn "/> (forgive me for the strange drawing wind ...) )
the following all back to assume that you do not have much knowledge of Linux to do, such as a master, the face of the time please light. +_+


1. File descriptors
The following is a description of the Linux shell script Raiders ( if there is infringement I will delete the t_t):

The file descriptor is an integer associated with the file input and output. They are used to track files that have been opened. The most common file descriptors are Stidin, stdout, and stderr. We can redirect the contents of a file descriptor to another file descriptor.
"Linux shell Script Raiders"


File descriptor Our common is the system reserved 0,1 and 2 of these three, their meanings are the following corresponding relationship:

    • 0--stdin (Standard input)

    • 1--stdout (Standard output)

    • 2--stderr (standard error)


One of the most common uses of Shell programming is descriptor 1, and descriptor 2. So let's raise two chestnuts and know that the horse is 1 and 2:
1--stdout
Suppose: In the current directory we "Have and only" a text file named 123.txt. This time we run the following command "LS 123.txt":

<img src=\ ' #\ ' "//pic4.zhimg.com/v2-8830db673e8054c3050bc47f3d083c73_b.png& quot data-rawwidth="622" data-rawheight="158" Class="origin_image zh-lightbox-thumb" width="622" data-original="https://pic4.zhimg.com/v2-8830db673e8054c3050bc47f3d083c73_r.png& Quot;> we will get a 650) this.width=650; "Src=" https://pic4.zhimg.com/ V2-8830db673e8054c3050bc47f3d083c73_b.png "class=" origin_image zh-lightbox-thumb lazy "width=" 622 "style=" width : 584px;height:148.347px; "alt=" v2-8830db673e8054c3050bc47f3d083c73_b.pn "/> We're going to get a Standard output stdoutOutput of "123.txt".
2--stdout
On the same assumption as above, we run another hop command "LS abc.txt":
<img src=\ ' #\ ' "//pic1.zhimg.com/v2-32eebce74f8dbe4f8ea6c1a3cee4684c_b.png& quot data-rawwidth="608" data-rawheight="112" Class="origin_image zh-lightbox-thumb" width="608" data-original="https://pic1.zhimg.com/v2-32eebce74f8dbe4f8ea6c1a3cee4684c_r.png& Quot;> we will get a 650) this.width=650; "Src=" https://pic1.zhimg.com/ V2-32eebce74f8dbe4f8ea6c1a3cee4684c_b.png "class=" origin_image zh-lightbox-thumb lazy "width=" 608 "style=" width : 584px;height:107.579px; "alt=" v2-32eebce74f8dbe4f8ea6c1a3cee4684c_b.pn "/> We're going to get a standard error stderrOutput "LS: Cannot access abc.txt: No file or directory"
Some students should think that these two cases seem to have nothing to do with the two Arabic numerals of 1 and 2. This will be combined with the second point of knowledge "redirect operation" to understand.
2. REDIRECT Operation
There is no accurate description of the redirect in the book, I do not want to face the drip to say I understand it. The redirect operation is to re-enter the text information for the file descriptor output of the shell command into another specified file by appending a redirect action symbol to the shell command.
The redirect operation symbol has two > and >>. Although both operators can redirect to a file, the former empties the file and then writes the content, which appends the content to the end of the existing file. (by the way, if the redirected operation is not already present, the redirected action will actively create the file of the file name)
Below we combine the 1th knowledge point file descriptor to lift the chestnut bar.
REDIRECT standard output stdout
<img src=\ ' #\ ' "//pic2.zhimg.com/v2-344ea7a024ecd442d39ab9f6e68ff0ad_b.png& quot data-rawwidth="557" data-rawheight="136" Class="origin_image zh-lightbox-thumb" width="557" data-original="https://pic2.zhimg.com/v2-344ea7a024ecd442d39ab9f6e68ff0ad_r.png& Quot;> as shown, the LS command does not print the character "123.txt" to the screen after it has been used, as compared to the operation without adding a redirect. After the cat operation, we can see that the output string should have been recorded in the Stdout.txt file. 650) this.width=650; "src=" Https://pic2.zhimg.com/v2-344ea7a024ecd442d39ab9f6e68ff0ad_b.png "class=" origin_image Zh-lightbox-thumb lazy "width=" 557 "style=" WIDTH:557PX;HEIGHT:136PX; "alt=" V2-344ea7a024ecd442d39ab9f6e68ff0ad_ B.pn "/> As shown, the LS command does not print the character" 123.txt "to the screen after it is used, as compared to the operation without adding a redirect. After the cat operation, we can see that the output string should have been recorded in the Stdout.txt file.
In fact, the redirection operation for standard output,> is equivalent to 1>. Above chestnuts execute command "ls 123.txt > stdout.txt" Get the same effect.
REDIRECT standard error stderr
<img src=\ ' #\ ' "//pic3.zhimg.com/v2-cf091eefd7ca4eca5dc44dc8ba5bf8ee_b.png& quot data-rawwidth="545" data-rawheight="134" Class="origin_image zh-lightbox-thumb" width="545" data-original="https://pic3.zhimg.com/v2-cf091eefd7ca4eca5dc44dc8ba5bf8ee_r.png& Quot;> as shown, the file descriptor 2, the standard error redirection is the same principle is recorded in the file stderr.txt this file. 650) this.width=650; "src=" Https://pic3.zhimg.com/v2-cf091eefd7ca4eca5dc44dc8ba5bf8ee_b.png "class=" origin_image Zh-lightbox-thumb lazy "width=" 545 "style=" WIDTH:545PX;HEIGHT:134PX; "alt=" V2-cf091eefd7ca4eca5dc44dc8ba5bf8ee_ B.pn "/> As shown, file descriptor 2, standard error redirection is the same principle is recorded in the file stderr.txt this file.


There are several uses for descriptor redirection:
You can direct stderr to a single file and redirect stdout to another file:

CMD 2>stderr.txt 1>stdout.txt

You can also use the following method to convert stderr to stdout, so that both stderr and stdout are re-clove into the same file:

Cmd> output.txt 2>&1

Or use this method (this is often used, I personally prefer to use this, less write a few characters (*^__^*))

CMD &> output.txt

(Finally, finally a knowledge point, the original serious answer code word so. Fall ~)
3. Linux Special files
A piece of the "Linux shell script Raiders" description:

/dev/null is a special device file, and any data received by this file will be discarded. Therefore, null this device is often also used as a bit bucket (bit buckets) or black hole.

It is simply understood that all the things redirected to this/dev/null file will be discarded.
Because these file descriptor output strings are always displayed. If we are in the shell programming, the operation to the return of a certain command, we do not want this time and do not want this output to print to the screen (printing error, how not good ^_^) we can redirect to/dev/null this file, from/dev /null This document is responsible for handling the funeral.
The result of this discard cannot be rudely considered to be the deletion of the error output, which is an operation that discards the redirected input and output. The image of the understanding is that the ATM machine printing paper Journal (stdout and stderr) should have you to save the processing, but you do not have to put in the hand (print screen) and the way, so the bill from your hand to be thrown back into the dustbin (/dev/null). But you don't know how the garbage can be handled.
Do not know the above description, answer whether the Lord can understand these three points of knowledge?
As long as you understand the above three points, actually answer the third question of the Lord is very good to solve.

The idea of question 3
Let a variable get the result of the command output, is the following processing:

i=$ (LS 123.txt)

In this way, I can get all the results of the command "LS 123.txt" output on the screen. Whether the output is a stdout output or a stderr error prompt. I can get a string from this variable.
For the answer to the main question, the following should be done:

i=$ (LS 123.txt 2>/dev/null)

With this command, the LS command will be redirected to the/dev/null trash if there is an error message. Therefore, I this variable can not get to the STDERR standard error prompt string. Therefore, in the operation of this command, I can only get the file stdout standard output, that is, the file Descriptor 1 screen output "123.txt".
If, this 123.txt file does not exist, I will not get anything because the error message was/dev/null eaten (crossed out), was redirected discarded. So, I is a null variable with nothing at all. The basic effect is as follows:

I= "

This article is from the "Star Wind Memories" blog, please be sure to keep this source http://yuyongid.blog.51cto.com/10626891/1881691

The shell output of the garbage bin--/dev/null

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.