Shell redirection under Linux

Source: Internet
Author: User

1. Standard input, standard output and standard error output

Linux system open 3 files, standard input, standard output, standard error output.

Standard input: Enter data from the keyboard, i.e. read data from the keyboard.

Standard output: Outputs the data to the terminal.

Standard error Output: Outputs the standard error to the terminal.

The default standard input refers to the keyboard, the default standard output and the standard error output refer to the screen or the terminal.

The system assigns the file identifier FD (Descripter) to these three files.

In the Linux system, everything is a file, the operation of the file, the general need to use the file identifier. Their file identifiers, respectively, are 0,1,2.

2. Redirection

Redirection is divided into input redirection and output redirection.

The so-called input redirection is that the input is not read from the keyboard, but is entered from the file or other.

The so-called output redirection is not output to the terminal, but output to a file or other.

"<" represents the input redirection operator, and ">" represents the output redirect T operator.

The input "<<" tells the shell that the current standard enters the middle of a pair of separators from the command line.

">>", with ">" Output, if the file does not exist will automatically create a file, if the second input, will overwrite the previous input, and ">>" means to append the second output to the file, rather than overwrite.

3. Take a look at a few examples below

(1) Input redirection

[Email protected] ~]# Wc</etc/inittab
53 229 1666

WC statistics Number of rows, words, characters

Where the WC input comes from/etc/inittab


[Email protected] ~]# WC <<AA
> 1
> 11
> 111
> AA
3 3 9

The WC statistic separators the contents between AA.

(2) Output redirection

[[email protected] ~]# Ps-ef >a.txt//redirect the information output of the process to the A.txt file

[[email protected] ~]# ps-ef> >a.txt//process information appended to the A.txt file

In fact, the so-called input and output redirection is not from the keyboard input and screen output.

4. Typical analysis of output redirection under Linux

(1) a.out > outfile 2>&1
(2) a.out 2> &1 >outfile

Analysis:

0 for standard input, 1 for standard output, and 2 for standard error output. where 1 (i.e. >=1>) can be omitted, the default is standard output.

(1) First redirect the a.out to the outfile file. The 2>&1 then redirects the standard error output to the standard output. The standard output is redirected at this point, which is equivalent to redirecting the standard output to the outfile with the standard error output. &1 represents the file descriptor for the standard output. 1 generally on the left side indicates where the standard output is redirected, and 1 on the right is redirected to the standard output, so use & to differentiate the file descriptor representing the standard output. 2>1 indicates that the standard error is redirected to file 1, and 2>&1 indicates that the standard error is redirected to the standard output.

(2) The standard error output is first redirected to the standard output, at which point the standard output is still in the terminal. Standard errors are therefore also in the terminal. Then the standard output goes to the outfile. The standard error is still in the terminal.

Therefore, (1) indicates that standard output and standard errors are outfile.

(2) indicates standard output to outfile, but the standard error output is still in the terminal.

LS 1>/dev/null 2>/dev/null

LS >/dev/null 2>&1

/dev/null is a trash bin that is a bottomless pit that represents the meaning of not showing.

That is, the standard output is not displayed with the standard error output.

The above two commands represent the same meaning.

The above is about the redirection problem under Linux, we must understand the principle deeply.

Original address: http://blog.csdn.net/jfkidear/article/details/7823486

Shell redirection under Linux

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.