>/Dev/null 2> & 1 solves annoying standard input

Source: Internet
Author: User

Why use/dev/null 2> & 1

This method is used. this command redirects all standard output and error output to/dev/null, that is, it discards all generated information. next, let me talk about it for you,Command
> File 2> File

AndCommand> file 2> & 1
What's the difference.
First ~Command> file 2> File

The standard output information and error output information generated by the command are sent to the file.Command> file 2> File
Stdout
And stderr
Both
Directly to the file, the file will be opened twice, so that stdout
And stderr
It will overwrite each other. In this way, both fd1 and fd2 are used to seize the file pipeline at the same time.
WhileCommand> file 2> & 1
This command will
Directly send to file, stderr
After inheriting the fd1 pipeline, it is sent to file again. At this time, the file is opened only once, and only one pipeline fd1 is used, which includes stdout
And stderr
.
In terms of Io efficiency, the efficiency of the previous command is lower than that of the subsequent command. Therefore, when writing shell scriptsCommand
> File 2> & 1

This method is used.
**************************************** ************
In Unix
0 = stdin
1 = stdout
2 = stderr

>/Dev/null 2> & 1 means
Redirect all the standard out and standard error messages/output/results
From the programs/scripts to/dev/null (which means they go to
Bottomless Pit)

**************************************** *************

/Dev/null 2> & 1


You need to understand the theory first and then its upto you how and
Where you want to apply that theory. I'll try to explain ababve to you.

The greater-than (>) in commands like these redirect the program's
Output somewhere. In this case, something is being redirected
/Dev/null, and something is being redirected into & 1.

Standard In, out and error:

There are three standard sources of input and output for a program.
Standard Input usually comes from the keyboard if it's an interactive
Program, or from another program if it's processing the other program's
Output. The program usually prints to standard output, and sometimes
Prints to standard error. These three file descriptors (you can think
Them as "data pipes") are often called stdin, stdout, and stderr.

Sometimes they're not named, they're numbered! The built-in Numberings
For them are 0, 1, and 2, in that order. By default, if you don't name
Or number one explicitly, you're talking about stdout.

That means file descriptor 0 or fd0 denotes stdin or standard input and
File descriptor 1 or fd1 denotes stdout or standard output and file
Descriptor 2 or fd2 denotes stderr or standard error.

You can see the command above is redirecting standard output
/Dev/null, which is a place you can dump anything you don't want (often
Called the bit-bucket), then redirecting standard error into standard
Output (You have to put an & in front of the destination when you do
This ).

The short explanation, therefore, is "all output from this command
Shocould be shoved into a black hole. "That's one good way to make
Program be really quiet!

**************************************** *****
When in a shell, you can do varous things with the output and input
Commands. You can run them through a pipe:

Grep root/var/log/messages | less

This takes the output of the 'grep' commands, and uses the 'none'
Paginator to finally display it on the screen.

To go into further detail, this sends the stdout of 'grep' to less.

If you wanted to send the output to a file, you 'd use a redirect:

Grep root/var/log/messages>/tmp/File

This takes the stdout, and sticks it into '/tmp/file'. I won't go
Detail about the> and> options etc. As they are all covered
In the man page.

There are 3 file descriptors for every process. stdin, stdout and
Stderr. These map to 0, 1 and 2 respecitvly.

Strangely enough, the above command can also be written:

Grep root/var/log/messages 1>/tmp/File

The First Command makes the assumption that you want to redirect stdout.
This one clears up that assumption.

Now we'll look at stderr:

Grep root/var/log/mseeagse>/tmp/File

This will spit out an error to your tty, even though you 've redirected
The output.

Grep root/var/log/mseeagse 2>/tmp/File

This one will output nothing, and '/tmp/file' will contain in the error. If
We didn't miss-spell the messages file name, any errors that might
Occur wocould go to the file, but the output will go to the TTY.

Now we'll do the last confusing thing, moving a file descriptor to
Different one. This is where the & <number> comes in:

Grep root/var/log/mseeagse 2> & 1

This moves stderr so it comes out in the same place as stdout. It says
"Move file descriptor 2 to file descriptor 1 ".

Thus where we get to your tail. We mix and match the stdout and stderr
Redirections:

Grep root/var/log/messages>/tmp/file 2>
& 1
Grep root/var/log/mseeagse>/tmp/file 2> & 1

With both of these commands, both the regular output (stdout), and any
Errors that occur (stderr) will go into '/tmp/file '.

When stacking redirects like this, there is one major thing to be aware
Of. When you move a file descriptor, you have to make sure
Descriptor you are moving too has already been redirected to where you
Want, otherwise you can get some... interesting results.
**************************************** ********************
This is from memory.
Everything in Linux is a file, including I/O. There are three standard
File descriptors, standard in (stdin, file descriptor 0), Standard
Output (stdout, file descriptor 1) and standard error (stderr, file
Descriptor 2)./dev/null is the null device, which is like "write only
Memory ".> will write to the specified file (overwriting its
Contents) and> will append to the specified file.

2> redirects stderr to the specified file.> is used to append
To the end of the file.

& Only means to run the process in the background if it appears
The end of the line.

2> & 1 redirects stderr to stdout. Since in this case, stdout is
Being redirected to/dev/null, 2> & 1 causes both stderr and stdout
To/dev/null.

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.