Linux shell data Redirection (input redirection and output redirection) detailed analysis (reprint)

Source: Internet
Author: User
Tags stdin

This article is a reprinted original link http://www.cnblogs.com/chengmo/archive/2010/10/20/1855805.html

Before understanding redirection, let's take a look at the file descriptor of linux.

Linux file descriptor: It can be understood that Linux tracks open files, and a number is assigned. This number is a bit similar to the handle when the C language operates the file. The file can be read and written through the handle. The user can customize the file descriptor range: 3-num, the maximum number, which is related to the user's: ulimit -n definition number, and cannot exceed the maximum value.

 

After Linux starts, it will open three file descriptors by default, namely: standard input standard input 0, correct output standard output 1, error output: error output 2

After opening the file later. New file binding descriptors can be added in sequence. A shell command will inherit the file descriptor of the parent process. Therefore, all shell commands run will have three default file descriptors.

 

For any linux command execution, it will be such a process:

technology sharing

A command was executed:

There is an input first: input can be obtained from the keyboard or from the file

Command execution completed: successful, will output the successful result to the screen: standard output is the screen by default

There is an error in the execution of the command: the error will also be output to the screen: standard error also refers to the screen by default

 

File input and output is accomplished by tracking the integer handles of all open files for a given process. These numeric values are the file descriptors. The most known file descriptors are stdin, stdout, and stderr, and the file descriptor numbers are 0, 1, and 2, respectively. These numbers and individual equipment are reserved. Before a command is executed, all input and output will be prepared. By default, they are respectively bound (stdin, stdout, stderr). If an error occurs at this time, the command will be terminated and will not be executed. Command parsing process, you can refer to: Linux Shell wildcard, metacharacter, escape character use case introduction

 

These default outputs and inputs are all set by the Linux system. During our use, we sometimes do not want to output the execution results to the screen. I want to output to a file or other device. At this time, we need to redirect the output.

 

Common input and output operators under the Linux shell are:

1. Standard input (stdin): the code is 0, use <or <<; / dev / stdin-> / proc / self / fd / 0 0 means: / dev / stdin
2. Standard output (stdout): The code is 1, use> or >>; / dev / stdout-> / proc / self / fd / 1 1 means: / dev / stdout
3. Standard error output (stderr): code is 2, use 2> or 2 >>; / dev / stderr-> / proc / self / fd / 2 2 represents: / dev / stderr

 

Output redirection:
format:

command-line1 [1-n]> file or file operator or device

The above command means: the result of executing a command (standard output, or error output, which should be printed on the screen) is redirected to other output devices (file, open file operator, or printer, etc.) 1,2 Standard output, error output.

Examples:


#Display the current directory file test.sh test1.sh test1.sh does not actually exist
[chengmo @ centos5 shell] $ ls test.sh test1.sh
ls: test1.sh: no such file and directory
test.sh
 
#The correct output and error output are displayed on the screen, now you need to write the correct output to suc.txt
# 1> Can be omitted, not written, default to the standard output
[chengmo @ centos5 shell] $ ls test.sh test1.sh 1> suc.txt
ls: test1.sh: no such file and directory
[chengmo @ centos5 shell] $ cat suc.txt
test.sh
 
#Output error, not output to screen, output to err.txt
[chengmo @ centos5 shell] $ ls test.sh test1.sh 1> suc.txt 2> err.txt
[chengmo @ centos5 shell] $ cat suc.txt err.txt
test.sh
ls: test1.sh: no such file and directory
#Continue appending to write output to suc.txt err.txt ">>" append operator
[chengmo @ centos5 shell] $ ls test.sh test1.sh 1 >> suc.txt 2 >> err.txt
 
#Turn off the error output information
[chengmo @ centos5 shell] $ ls test.sh test1.sh 2> &-
test.sh
[chengmo @ centos5 shell] $ ls test.sh test1.sh 2> / dev / null
test.sh
# & [n] represents the existing file descriptor, & 1 represents the output & 2 represents the error output &-represents close the descriptor bound to it
# / dev / null This device is a black hole device in Linux, and any information will be eaten as long as it is output to this device
 
#Close all output
[chengmo @ centos5 shell] $ ls test.sh test1.sh 1> &-2> &-
#Close 1, 2 File descriptor
[chengmo @ centos5 shell] $ ls test.sh test1.sh 2> / dev / null 1> / dev / null
#Forward the output of 1,2 to the / dev / null device
[chengmo @ centos5 shell] $ ls test.sh test1.sh> / dev / null 2> & 1
#Bind the error output 2 to the correct output 1, and then send the correct output to the / dev / null device.
<p> [chengmo @ centos5 shell] $ ls test.sh test1.sh &> / dev / null
# & Stands for standard output and error output. Input all standard output and error output to the / dev / null file
</ p>
 

 

note:

1. The shell encounters the ">" operator, it will determine whether the file on the right exists, delete it first, and create a new file. There is no direct creation. Regardless of whether the left command is executed successfully. The files on the right will become empty.

2. ">>" operator, judge the right file, if it does not exist, create it first. If you open the file by adding, a file descriptor will be assigned [not specified, the default is 1, 2]. Then, it is bound to the standard output (1) or error output (2) on the left.

3. When the command is executed, the descriptor of the binding file is automatically invalid. 0,1,2 will be idle again.

4. A command is started, the command input, correct output, and error output are bound to 0, 1, 2 file descriptors by default.

5. Before executing a command, it will check whether the output is correct. If the output device is wrong, the command will not be executed.

Input redirection
format:

command-line [n] <file or file descriptor & device

Of course, the command input from the keyboard by default is changed to input from a file, or other open files and devices. Execute this command to bind standard input 0 to the file or device. It will be input by it.

Examples:


[chengmo @ centos5 shell] # cat> catfile
testing
cat file test
#Press [ctrl] + d to leave here
#Get data from standard input [keyboard] and output to catfile
 
[chengmo @ centos5 shell] $ cat> catfile <test.sh
#cat Obtain input data from test.sh and output to file catfile
 
 
[chengmo @ centos5 shell] $ cat> catfile << eof
test a file
test!
eof
 
# << This two consecutive small symbols, it means "end input character". In this way, when you enter the EOF character on a blank line, the input ends automatically without ctrl + D
 

 

exec binding redirect
format:

exec file descriptor [n] <or> file or file descriptor or device

In the above-mentioned input, output redirection will be input, after output binding file or device. Only valid for the current instruction. If you need to support all the following commands after binding. You need to use the exec command

Examples:


[chengmo @ centos5 shell] $ exec 6> & 1
#Bind standard output to fd 6
 
[chengmo @ centos5 shell] $ ls / proc / self / fd /
0 1 2 3 6
#File descriptor 6 appears
 
[chengmo @ centos5 shell] $ exec 1> suc.txt
#Standard output of all subsequent commands, bind to suc.txt file (output to this file)
 
[chengmo @ centos5 shell] $ ls -al
#Execute the command and find that nothing is returned, because the standard output has been output to the suc.txt file
 
[chengmo @ centos5 shell] $ exec 1> & 6
#Recover standard output
 
 
[chengmo @ centos5 shell] $ exec 6> &-
#Close fd 6 descriptor
 
[chengmo @ centos5 ~] $ ls / proc / self / fd /
0 1 2 3
Note: Save the standard input to file descriptor 6 before use. Here, the file descriptor will be opened by default 0,1,2, and you can also use a custom descriptor Then bind the standard output to a file, and then all output will occur to the file. After use, restore the standard output and close the open file descriptor 6.

Interesting things:

Some friends may use it like this: exec 1> suc.txt, then all the output is bound to the suc.txt file, so how to restore the original? Try it and you will find the problem ...

A little more complicated example

exec 3 <> test.sh;
#Open test.sh readable and writable operation, bound to file descriptor 3
 
while read line <& 3
 do
    echo $ line;
done
#Cycle reading file descriptor 3 (the content of test.sh is read)
exec 3> &-
exec 3 <&-
#Close the file, input and output binding
 

To sum up:
Learning must be summed up before it can be improved. Haha!

It is estimated that some friends are dizzy. How is the redirection of Linux so complicated, it is the file open descriptor and the read, and there are some, and the default standard input and output.

In fact, to summarize, the redirect application usually has the following two points:

1. Reset the default input and output of the command to point to your own files (files, file descriptors, and devices are actually files, because Linux is based on devices and files, and descriptors also point to files, haha)

2. Expand your own new descriptor to read and write files

Linux shell data redirection (input redirection and output redirection) detailed analysis (reproduced)

label:

Original address: http://www.cnblogs.com/yiruhua/p/5483201.html

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.