Tips for Linux I/O redirection

Source: Internet
Author: User
Level: elementary

Fan Qi (fanqi@cn.ibm.com)
Xseries eserver engineer, ibm tss China Beijing

Although Linux I/O redirection is very simple, it is often useful to understand the usage skills in script writing and system management.

First, let's talk about what is I/O redirection. The so-called I/O redirection is simply a process, which captures a file, or commands, programs, scripts, even the code block output in the script sends the captured output to another file, command, program, or script as the input.

When it comes to I/O redirection, it involves the concept of a file identifier (file descriptor). in Linux, the system specifies a file identifier for each opened file so that the system can track the file, this is similar to the file handle in C programming. The file identifier is a number. Different numbers indicate different meanings. By default, the system occupies three, 0 standard input (stdin), 1 standard output (stdout), 2 standard error (stderr), and 3-9 are reserved identifiers, you can specify these identifiers as standard input, output, or errors as temporary connections. This usually solves many complicated redirect requests.

Standard Input usually refers to the keyboard input
Standard output usually refers to the output of the monitor.
Standard errors are usually directed to the display.

Take the following example to describe their relationship

#ls /dev

This command lists all the files in the/dev directory and outputs the results on the screen.

Here/Dev is the standard input (input from the keyboard) of the LS command, and the result printed on the screen is the standard output (content in the/dev directory)

Return to the title. redirection is to change the standard input or output to another method. See the following example.
Or equivalent

# Ls/dev 1> filename # Note: "1" and ">" have no spaces in the middle

The above command will redirect the standard output of the command to a file filename instead of display on the screen. If the file identifier is not specified, the default value is 1, so 1 can be omitted.

If you change ">" to ">" in the preceding example, the output is appended to the end of the filename file. If the file does not exist, it is created. As follows:

#ls /dev >>filename 

You can also redirect standard errors to files.

#ls -qw  /dev  2>filename

Apparently-qw is an error parameter and usually reports an error message on the monitor. However, because stderr is redirected to the filename file again, the monitor has no error message, the information is written to the file.

The following command directs both standard output and errors to the file

#ls /dev &>filename

"&" Indicates the standard output and standard error. Both the normal output and the error message are written to filename.

Redefinition of standard input, output, and incorrect file identifiers

You can use the I> & j command to redirect the File Identifier I to j. You can think of "&" as "Get address"

See the following example.

#exec 5>&1

Object Identifier 5 is directed to the standard output. This command is usually used to temporarily Save the standard input.

The standard input can also be reoriented. refer to the following example.

# grep search-word 
      

In general, the grep command searches for strings in a given file. The above command uses the file filename as the standard input for the grep command, rather than input from the keyboard.

As mentioned above, the system specifies a file identifier for each opened file so that the system can track the file. What is the default file identifier? The answer is 0, that is, standard input, or input from the keyboard. Of course, this file identifier can also be specified by yourself. Please refer to the example below

# Echo 123456789> filename: Write the string to the filename file.
# Exec 3 <> filename: open the file filename and specify the File Identifier as 3.
# Read-n 4 <& 3 read four characters from the file. The handle has reached the end of the fourth character.
# Echo-n.> & 3: Write a dot at 5th characters, covering 5th characters.-n indicates no line break.
# Exec 3> &-close the file identifier

Now the result of the cat filename file is 1234.6789

Command j <> filename indicates opening the file and specifying the file ID as j
"&-" Indicates the identifier of the disabled file.

For more information about how to disable a file identifier, see the following.

N <&-Disable the Input File Identifier n
0 <&-or <&-Disable stdin
N> &-Disable output file identifier n
1> &-or> &-Disable stdout

There are other commands as follows:

2.:> filename or> filename

Indicates that the file filename is set to null, that is, the file content is cleared. If the file does not exist, an empty file is created (equivalent to the touch command): indicates an empty output, the only difference between the two commands is that> filename does not work properly in all shells.

References

* Jeffrey Friedl, Mastering Regular Expressions, O 'Reilly

* Mendel Cooper, Advanced Bash-Scripting Guide

* Michael Jang, Mastering RedHat 9

About the author
Fan Qi, ibm tss China Beijing, Xseries eServer engineer, very interested in Linux. You can contact us through a fanqi@cn.ibm.com. Tel: 010 84981188-6856

Full text from: IBM developerWorks Chinese website

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.