Small Ant learns Linux (--linux) output redirection and input redirection

Source: Internet
Author: User
Tags learn php

Speaking of output redirection, let's look at what the standard input is:

Device file name file descriptor type

     keyboards                                       /dev/stdin                         0                                       Standard input

     Monitor                                   /dev/sdout                         1                                       Standard output

Display/dev/sdterr 2 standard error output

Although in modern input device still have what mouse, game handle what, output device also has what projector printer, but in the earliest is a keyboard a monitor go world.


Output redirection

> Greater than, the content should be output to the screen displayed in the file, equivalent to keep the log (that is, the meaning)

For example, ls > Test.log keep the contents of the current directory in the Test.log. But this approach can only be overridden, and only the last redirect is visible.

>> double greater than, the same is the content should be output to the screen in the file, but this way is written in an append, the previous log content is retained, it is clear that this way than the coverage is much better.

However, there is a problem, when the command executes successfully, it can actually redirect the content to Test.log, but when the command does not exist or the command goes wrong, the redirect fails, how to redirect the command error message?

2> can redirect the error message to the log file, as well, a greater than sign is the meaning of the overlay.

For example: SLDFO (an arbitrarily written command) 2>test.log Note: Error message redirection when the greater than the number before and after the space.

2>> Ibid, but two greater than is the meaning of append.

For example: Sldjfs 2>>test.log Same, do not add space before and after the greater than number


In fact, both of these formulations have a disadvantage, without a 2 redirect can only execute a successful command, and with 2 redirects can only execute the failed command, this use is very tangled, very uncomfortable (??) nnn If you want to have a good rest, we recommend the following commands:

&> regardless of whether the command is executed correctly, the output information is redirected to the same file, of course, the old routine, which is covered.

Example: LS &> test.log

&>> Likewise, this is appended. This is more readily used, the right information is correct, the wrong information, the more worry ╰ ( ̄▽ ̄) ╭

The above two methods are recommended, in fact, there are two methods with the above two methods are the same effect, a bit verbose, know on the line:

Command > File 2>&1 Save the correct output and error output to the same file in a covered manner. function is completely equal to &>.

Command >> file    2>&1 in an additional way ... Functionally identical to &>>

Note: No matter which error output command, it is necessary to add a space before and after the greater than sign.

Yes, there's another way to use it.

Command >> file 1 2>> file 2 Append the correct output to file 1, and append the wrong output to the file 2, the feeling is clearer.


Input redirect

In contrast to >, input redirection is less than <. You can test it with the WC command.

WC "option" "File name"

Option-C Statistics bytes-W Statistics number of words-l count rows

For example: WC < Test.log will show the number of bytes, number of words, number of lines.

Input redirection is not commonly used, see.

learn PHP's small ant original blog http://my.oschina.net/woshixiaomayi/blog



Small Ant learns Linux (--linux) output redirection and input redirection

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.