Shell input and output redirection __linux under Linux

Source: Internet
Author: User
on the shell:
0 indicates standard input, input data from the terminal, generally refers to the keyboard
1 indicates the standard output, the data displays to the terminal, generally refers to the screen
2 indicates standard error output, Error prompts display to terminal, generally refers to screen
> default to standard output redirection, same as 1>

2>&1 means redirecting the standard error output to the standard output.

2>1 indicates that the standard error is redirected to file 1, using & to differentiate between the file name and the filename descriptor

&>file means redirecting both standard output and standard error output to file files


<,<<,>,>> Difference

"<" indicates the input redirection operator, and ">" represents the output redirection t operator.

"<<" This input tells the shell that the current standard input is from the command line to the start tag and the end tag tag between the contents as input.

> Output, a file is automatically created if the file does not exist, overwriting the previous entry if the second time.

">>" means to append the second output to a file instead of overwriting it.



PS EF > A.txt redirects the execution results of the PS command to the A.txt file, not on the screen.

Wc<a.txt WC originally directly statistics from the terminal input lines of text information (line number, word number, character number), here will be input redirect to A.txt file


Example 1 <<:

pp@pp-satellite-l700~$ cat << EOF
> Hello
> What?
> OK
> Bye
> EOF
Hello
what?
OK
bye
pp@pp-satellite-l700$ cat >test.txt << EOF
> Hello
> What?
> OK
> Bye
> EOF
pp@pp-satellite-l700$ cat test.txt 
Hello
what?
OK
bye
The first command: Cat <<eof, indicating an end to EOF, where the contents are transmitted to the cat as input.
The second command: Cat > Test.txt << EOF, first redirect the output of the cat to the file Test.txt, and then redirect the input to the command line end with EOF as the end tag. Equivalent to Cat 1>test.txt 0<< EOF and Cat 0<<eof 1>test.txt

Example 2:
Note that for ease of understanding, you must set up an environment that enables the grep da* command to have normal output and error output, and then use the following command to generate three files, respectively:
grep da * > Greplog1
grep da * > Greplog2 1>&2
grep da * > Greplog3 2>&1//grep da * 2>greplog4 1>&2 results
#查看greplog1会发现里面只有正常输出内容
#查看greplog2会发现里面什么都没有

#查看greplog3会发现里面既有正常输出内容又有错误输出内容


Example 3:

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

/dev/null is a special file device, all input will be discarded. Used to filter out the output you don't want to see.


Reference:

Http://blog.sina.com.cn/s/blog_406127500102uxrs.html

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

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.