Linux input and output redirection

Source: Internet
Author: User
Tags stdin

Http://www.cnblogs.com/chengmo/archive/2010/10/20/1855805.html

in the Linux , when a user process is created, the system automatically creates three data streams for that process, which is the three mentioned in the topic. So what is Data flow (stream)? We know that a program to run, need to have input, output , if the error, but also to show their own errors . This is about reading data from somewhere and outputting it somewhere, which is enough to be a stream of data.

Thus, the three data streams that a process has initially had are standard output, standard input, and standard errors, respectively, using stdout, stdin, and stderr . For these three data streams, the default is shown on the user Terminal .

Linux when started, it opens by default 3 file descriptors, respectively: standard input Standard input 0, correct output Standard Output 1, Error Output: Error Output 2

After you open the file. The new file binding descriptor can be incremented in turn. a shell Command executes, inheriting the file descriptor of the parent process. Therefore, all running shell commands will have a default of 3 file descriptors.

A command was executed:

There is one input: the input can be from the keyboard, or the file can be

command execution completed: successful, will output the success of the results to the screen: Standard Output The default is the screen

Command execution error: The error is also output to the screen: Standard Error The default also refers to the screen

file input and output is traced to a Process to complete the integer handle of all open files. These numeric values are file descriptors. The most known file meter descriptor is the   stdin ,   stdout   and   stderr 0 1 and 2 If there is an error at this time, the command will be terminated and will not be executed. Command parsing process, you can refer to: linux Shell wildcard, metacharacters, Escape Character Usage Example introduction

These default outputs, the inputs are all Linux System-Default, we in the use process, sometimes do not want to execute the results output to the screen. I want to output to a file or other device. At this point we need to redirect the output.

The common input and output operators under the Linux shell are:

1 [[email protected] ~]#2 lrwxrwxrwx 1 root root-20:48/dev/stdin-/pro c/self/fd/03 [[email protected] ~]#  ll/proc/self/fd/04 lrwx------1 root Root 19:36/proc/self/fd/0-/dev/pts/0
View Code

11. Standard input (stdin): code 0, using < or <</dev/stdin,/proc/self/fd/0 0 for:/dev/stdin22. Standard output (STDOUT): Code 1, using > or >>/dev/stdout,/PROC/SELF/FD/1 1 for:/dev/stdout33. Standard error Output (STDERR): Code 2, using 2> or 2>>;/dev/stderr,/PROC/SELF/FD/2 2 for:/dev/stderr4 5 #Displays the current directory file test.sh test1.sh test1.sh actually does not exist6 [email protected] shell]$ ls test.sh test1.sh7 ls:test1.sh: No this file and directory8 test.sh9  Ten #The correct output and error output are displayed on the screen, now you need to write the correct output to Suc.txt One #1> can be omitted, not written, default to standard output A[[email protected] shell]$ ls test.sh test1.sh 1>Suc.txt - ls:test1.sh: No this file and directory - [email protected] shell]$ cat Suc.txt the test.sh -   - #error output, not output to screen, output to Err.txt -[[email protected] shell]$ ls test.sh test1.sh 1>suc.txt 2>Err.txt + [email protected] shell]$ cat suc.txt err.txt - test.sh + ls:test1.sh: No this file and directory A #continue append output to Suc.txt err.txt ">>" Append operator at[[email protected] shell]$ ls test.sh test1.sh 1>>suc.txt 2>>Err.txt -   - #shut down error output information -[Email protected]centos5 shell]$ ls test.sh test1.sh 2>&- - test.sh -[[email protected] shell]$ ls test.sh test1.sh 2>/dev/NULL in test.sh - #&[n] Represents a file descriptor that already exists, &1 represents the output &2 represents an error output &-A descriptor that is closed to it to #/dev/null This device, is the black hole device in Linux, what information as long as the output to this device, will be eaten +   - #turn off all output the[[email protected] shell]$ ls test.sh test1.sh 1>&-2>&- * #Close 1, 2 file descriptor $[[email protected] shell]$ ls test.sh test1.sh 2>/dev/null 1>/dev/NULLPanax Notoginseng #forward the output to the/dev/null device -[[email protected] shell]$ ls test.sh test1.sh >/dev/null 2>&1 the #bind the error output 2 to the correct output 1, and then send the correct output to the/dev/null device, a common +<p>[[email protected] shell]$ ls test.sh test1.sh &>/dev/NULL A #& represents standard output, error output input all standard output and error output to/dev/null file the</p> +  -&>/dev/null equivalent to >/dev/null 2>&1
View Code

Linux input and output redirection

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.