Level: primary
Fan Qi (
Fanqi@cn.ibm.com)
XSeries Eserver Engineer, IBM TSS China Beijing
Although Linux I / O is very simple, it is often dealing with the script, and system management is often dealing with it, and it is very useful.
First, what is I / O redirection, so-called I / O redirection is a process, this process captures a file, or the command, program, script, and even code blocks in the script (Code block), Then put the captured output as an input to another file, command, program, or script.
If you talk about the I / O redirection, it involves the concept of file design (File Descriptor), in the Linux system, the system specifies a file identifier for each open file so that the system is tracking files, here are some and C The file handle in the language program is similar, the file identifier is a number, and the different numbers represent different meanings. By default, three are occupied by 3 standard inputs (STDIN), 1 standard output (STDOUT), 2 standards Error (stderr), additional 3-9 is the reserved identifier, which can specify these identifiers to standard input, output, or errors as temporary connections. Usually, this can solve a lot of complex redirect requests.
Standard input usually refers to the input of the keyboard
Standard output usually refers to the output of the display
Standard error is usually oriented to the display
Please see the following examples to describe their relationships
#ls / dev
This command lists all files in the / dev directory and outputs the result on the screen.
Here / dev is the standard input as a command LS (input from the keyboard), and the result of printing on the screen is the standard output (content in / dev directory)
Still returning to the title, the redirection is to change the standard input or output into other ways, please see the following example
Or equivalent
#LS / Dev 1> FileName # Note: No space in the middle of "1" and ">"
The above command will redirect the standard output of the command to a file filename instead of displaying the file, if the file identifier is not specified, the system default is 1, so 1 can be omitted
If the ">"> "" "" "" "" "" "" "is indicated to the end of the filename file, it is created if the file does not exist. as follows
#ls / dev >> filename
You can also realligate standard errors to files.
#LS -QW / DEV 2> FileName
Obviously -QW is an error parameter, usually reporting an error message on the display, but since the 2 Standard Error (STDERR) is re-directioned to the file filename, there is no error message, and the information is written in the file.
The following command is to direct standard output and errors to the file
#LS / Dev &> FileName
"&" Represents standard output and standard errors here, whether it is normal output or error messages.
Redefine standard input, output, and error file identifier
The redefile file identifier can use the I> & j command, indicating that the file identifier I is re-directioned to j, you can understand "&" as "take address"
Please see the following example #EXEC 5> & 1
Indicates that the file identifier 5 is oriented to a standard output, which is usually used to hold standard input.
The same standard input is also reordered, please refer to the example below.
# GREP Search-Word
Generally, the grep command searches for strings in a given file, or the above command inputs the file filename as the standard input of the GREP command, not from the keyboard.
As mentioned earlier, the system specifies a file identifier for each open file so that the system is tracking files, then what is the default file identifier? The answer is 0, that is, the standard input, or you can say it from the keyboard. Of course this file identifier can also be specified, please refer to the following example
#echo 123456789> FILENAME Write the string to file file filename #EXEC 3 <> FileName opens file filename, and specifies that the file identifier is 3 #Read -N 4 <& 3 reads 4 characters from the file, the handle already refers to The fourth character ends # echo -n.> & 3 Write a point at the 5 characters, overwrite the 5 characters, -N means not wrap #exec 3> & - Close the file identifier
Now the result of the Cat FileName file is 1234.6789
Command j <> filename indicates that the file is turned on and specifies that the file identifier is j.
"& -" indicates the closing file identifier
For the operation of turning off file identifier, please refer to the following
N <& - Close Input File Identifier N 0 <& - or <& - Close Standard Enter STDIN N> & - Turns Off Output File Identifier N 1> & - or> & - Close Standard Output STDOUT
There are also some other orders, as follows
2.:> Filename or> filename
Indicates that the file filename is set to empty, that is, the contents of the file, if the file does not exist, create an empty file, (equivalent to the touch command): Represents an empty output, the only difference between the two commands is> FileName is not in all shells Can work normally.
Reference
* Jeffrey Friedl, Mastering Regular Expressions, O'Reilly
* Mendel Cooper, Advanced Bash-Scripting Guide
* Michael Jang, MASTERING
Redhat 9
About author
Fan Qi, IBM TSS China Beijing, XSeries Eserver engineer, is very interested in Linux. by
Fanqi@cn.ibm.com can contact. Tel: 010 84981188-6856
Full article:
IBM DeveloperWorks China website