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

Source: Internet
Author: User
Tags stdin


Before we know about redirects, let's take a look at the Linux file descriptor.



Linux file Descriptor: Can be understood as a Linux trace open file, and the allocation of a number, this number is similar to the C language operation of the file when the handle, through the handle can be implemented to read and write files. The user can customize the file descriptor range is: 3-num, this maximum number, with the user's: ulimit–n definition of the relationship between the number, can not exceed the maximum value.






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



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






For any Linux command to execute, it would be such a process:






A command was executed:



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



Command execution completed: successful, will output the success results to the screen: standard output default is the screen
There is an error in the command execution: The error is also printed on the screen: standard error defaults to the screen



The file input output is completed by tracking the integer handle of all open files for a given process. These numeric values are file descriptors. The most widely known file meter descriptors are stdin, stdout and stderr, and the number of file descriptors is 0,1 and 2, respectively. These numbers and their respective devices are reserved. Before a command executes, all input outputs are ready, the default is bound (Stdin,stdout,stderr), and if there is an error, the command terminates and does not execute. Command parsing process, you can refer to: Linux Shell wildcard, metacharacters, escape character usages



These default output, input are the Linux system, we use the process, sometimes do not want to perform results output to the screen. I want to output to a file or other device. This time we need to do output redirection.



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



1. Standard input (stdin): code 0, using < or <</dev/stdin->/proc/self/fd/0 0 Representative:/dev/stdin
2. Standard output (STDOUT): Code 1, using > >>/dev/stdout->/proc/self/fd/1 1 representatives:/dev/stdout
3. Standard error Output (STDERR): Code 2, using 2> or 2>>;/dev/stderr->/PROC/SELF/FD/2 2 representatives:/dev/stderr



Output redirection:



Format:



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



The above command means: The result of a command execution (standard output, or error output, which would have to be printed to the top of the screen) redirects other output devices (file, open file operator, or printer, etc.) 1, 2 respectively is standard output, error output.



Instance:


##Show 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 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 the error, not to the screen, 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 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 error output
[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] stands for an existing file descriptor, & 1 stands for output & 2 stands for error output &-stands for closing the descriptor bound to it
# / dev / null This device is a black hole device in Linux. Any information output to this device will be eaten.
 
#Turn off all output
[chengmo @ centos5 shell] $ ls test.sh test1.sh 1> &-2> &-
#Close 1 and 2 file descriptors
[chengmo @ centos5 shell] $ ls test.sh test1.sh 2> / dev / null 1> / dev / null
# Forward 1, 2 output to / 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, error output Input all standard output and error output to / dev / null file


Attention:



1, the shell encountered the ">" operator, will determine whether the right file exists, if there is deleted first, and create a new file. No direct creation exists. Whether or not the command on the left is executed successfully. The right file will become empty.



2, ">>" operator, to determine the right file, if it does not exist, first created. Opening the file as an addition assigns a file descriptor [not specifically specified, defaults to 1, 2] and then binds to the standard output (1) or Error output (2) on the left.



3, when the command: execution, the binding file descriptor also automatically invalidated. 0,1,2 will be free again.



4, a command to start, the input of the command, the correct output, error output, the default binding 0,1,2 file descriptor respectively.



5, a command before executing, will check the output is correct, if the output device error, will not perform command



Input redirection



Format:



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



However, the command defaults to the input from the keyboard, which is changed from file, or other open file and device input. To execute this command, the standard input 0, binding to the file or device. will be entered by it.



Instance:


[chengmo @ centos5 shell] # cat> catfile
testing
cat file test
#Here press [ctrl] + d to leave
#Get data from standard input [keyboard] and output to catfile
 
[chengmo @ centos5 shell] $ cat> catfile <test.sh
#cat Get input data from test.sh and output to file catfile
 
 
[chengmo @ centos5 shell] $ cat> catfile << eof
test a file
test!
eof
 
# << This two small symbols in a row, which stands for "end of input character". In this way, when the eof character is entered on a blank line, the input ends automatically, without ctrl + D


exec binding Redirection



Format:



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



In the above mentioned input, output redirection will be input, output binding file or device after. is valid only for the current instruction. If you need to be bound, all subsequent commands are supported. You need to use the EXEC command.



Instance:


[chengmo @ centos5 shell] $ exec 6> & 1
#Bind standard output to fd 6
 
[chengmo @ centos5 shell] $ ls / proc / self / fd /
0 1 2 3 6
#Appears file descriptor 6
 
[chengmo @ centos5 shell] $ exec 1> suc.txt
#Bind all the following commands to the standard output and bind it to the 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
#Restore 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 the file descriptor 6 before use, the file descriptor will open 0,1,2 by default, and the custom descriptor can be used. The standard output is then bound to a file, and then all output occurs to the file. After use, restore the standard output and close the open file descriptor 6.



Interesting things:



There may be friends like this: exec 1>suc.txt, then all the output is bound to the Suc.txt file, then how to restore the original? If you try, you'll find the problem.



A bit of a complex example


EXEC 3 <> test.sh;
#Open test. SH read-write operation, with the file descriptor 3 bound while
read line <& 3 do
echo $ line;
Done
#Loop reading file descriptor 3 (read test.sh content)
exec 3> &-
exec 3 <&-
#Close the file, input, output binding 


In summary:



The study must summarize, the summary can raise. Ha ha!



It is estimated that some friends are dizzy. How Linux redirects are so complicated, the file-opening descriptor is read, and some, the default standard input output.



In fact, to sum up, redirect applications usually have the following two points:



1, reset the command of the default input, output, point to their own files (files, file descriptors, devices are actually files, because Linux is based on the device is also a file, the descriptor also points to the file, haha)



2. Expand your new descriptor to read and write files


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.