Linux redirection related (reprint post, for yourself Cookbook)

Source: Internet
Author: User
Tags stdin

Linux redirection refers to the modification of the original default of some things, the original system commands the default way to change, such as simple I do not want to see the output of the display but want to output to a file can be done through the Linux redirection to do this work.

The Linux default input is the keyboard and the output is the monitor. You can use redirection to change these settings. For example, when using the WC command is to manually enter a text to calculate the number of characters, with the redirection can be directly after a file has been written with ' < ' point to this command, you can directly count the number of characters of the file and so on. The output is the same, you can redirect the screen output to a file, and then to the file to see the results. The redirect operator can be used to redirect command input and output traffic from the default location to another location, where the input or output data stream is referred to as a handle, there are three common handles, and of course the handle can be extended on its own, and the general OS provides similar functionality. Handle description of handle handle code handle

STDIN 0 Keyboard input

STDOUT 1 Output information to the prompt window

STDERR 2 output error message to the prompt window

The default < redirect input operator is 0, and the default > redirect output operator is 1. After you type the < or > operator, you must specify a read-write location for the data, which can be a file name or other existing handle.

To specify redirection to an existing handle, use the & character followed by the handle number (that is, the & handle number) to redirect.

For example, the following command can redirect handle 2 (that is, STDERR) to handle 1 (that is, STDOUT): 2>&1

The following table lists the operators that can be used to redirect input and output data streams:

Linux redirection operator Function description

> Write command output to a file or device instead of a command prompt or handle

< read command input from a file instead of from the keyboard or handle

>> add command output to the end of a file without deleting information already in the file

>& writes the output of one handle to the input of another handle

<& reads input from one handle and writes it to another handle output

| Reads the output from one command and writes it to the input of another command, also known as the pipe operator

Now let's go back and look at the statement above Mysh > Mylog.txt 2>&1 can understand:

> Mylog.txt means to redirect the standard output to mylog.txt, equivalent to Mysh 1> mylog.txt;

2 >& 1 means that the error output is redirected to the handle 1 standard output, and that the standard output and error output generated during the execution of the Mysh command are redirected to the Mylog.txt;

The redirection function is very powerful, interested to try a variety of different combinations, look at the position of the front and back what will be the result?

Sometimes we may not want to record what standard output or error output, that can be used Mysh >null 2>null or Mysh >/dev/null 2>/dev/null;

I/O redirection detailed

1, the basic concept (this is the premise of understanding the knowledge behind, it must be understood)

A, I/O redirection is usually related to FD, the Shell's FD is usually 10, namely 0~9;

b, there are 3 commonly used FD, 0 (stdin, standard input), 1 (stdout, standard output), 2 (stderr, standard error Output), the default and keyboard, monitor, monitor;

C, the use of < to change the read Data channel (stdin), so that it from the specified file read into;

D, using > To change the Sent data channel (stdout, stderr), so that it output to the specified file;

E, 0 is the default value of <, so < is the same as 0<;,> is the same as 1>;

F, in IO redirection, stdout and stderr pipeline will be ready before the stdin read into the data;

G, piping "|" (Pipe line): The stdout of the previous command received the stdin of the next command;

H, Tee command is in the case without affecting the original I/O, the stdout copy to the file;

I, Bash (ksh) the process of executing commands: Parse command-variable Evaluation-command substitution ("and $)-redirect-wildcard expansion-Determine path-execute command;

J, put command group in Sub-shell to execute, also known as nested Sub-shell, it has a very important feature is: Inherit the parent shell's standard input, output, and error plus any o Ther open file descriptors.

K, EXEC command: Often used to replace the current shell and restart a shell, in other words, does not start the child shell. Any existing environment will be purged when this command is used. exec does not overwrite your current shell environment when working with file descriptors, and only then.

2. Basic IO

cmd > File redirect stdout to file;

cmd >> file redirect stdout to file (append);

CMD 1> fiel redirect stdout to file;

cmd > File 2>&1 redirect stdout and stderr to file files;

cmd 2> file redirects stderr to file;

cmd 2>> file redirects stderr to file (append);

cmd >> file 2>&1 redirect stderr and stderr to file (append);

The cmd < file >file2 cmd command takes the file file as the stdin, with File2 as the stdout;

Cat <>file Open File in a read-write manner;

cmd < file cmd command with file as stdin;

CMD << delimiter here document, read in from stdin until the delimiter delimiter is encountered.

3. Advanced IO

>&n uses the system invoke DUP (2) to copy the file descriptor N and use the result as standard output;

<&n standard input copied from file descriptor n;

<&-off standard input (keyboard);

>&-off standard output;

n<&-indicates that the n input is closed;

n>&-means the n output is closed;

All of the above can be preceded by a number, at which time the file descriptor is specified by this number instead of the default of 0 or 1. Such as:

... 2>file runs a command and directs the error output (file descriptor 2) to file.

... 2>&1 runs a command and merges its standard output and output. (Strictly speaking, the file descriptor 2 is created by copying the file descriptor, but the effect is usually to merge two streams.) )

We explain 2>&1 in detail: 2>&1 is fd2=fd1, this is not to say that the value of FD2 equals FD1 value, because > is to change the data channel sent, that is, FD2 "data output channel" to FD1 "data transmission Out channel ". If that's the case, the change doesn't seem to work, because the default output of FD2 and the default output of FD1 are all monitor, the same! However, when FD1 is another file, even other FD, this has a special purpose. Please make sure you understand this.

EXEC 0exec 1>outfilename # opens file Outfilename as stdout.

EXEC 2>errfilename # opens file Errfilename as stderr.

EXEC 0<&-# closes FD0.

EXEC 1>&-# closes FD1.

EXEC 5>&-# closes FD5.

This completes the learning of Linux redirection.

1 redirects


1.1 Redirect Symbol

> Output redirected to a file or device overwriting the original file
>! Output redirected to a file or device forcing overwriting of the original file
>> output redirected to a file or device to append the original file
< redirect input to a program

1.2 Standard error redirect Symbol

2> redirects a standard error output to a file or device overwriting the original file B-shell
2>> redirect a standard error output to a file or device appended to the original file
2>&1 redirect a standard error output to the standard output note: 1 could be the standard output.
>& redirect a standard error output to a file or device to overwrite the original file C-shell
|& to send a standard error pipe to another command as input

1.3 Command Redirection Example

In the execution of the Bash command, there are three main output conditions, namely:
1. Standard input, code 0, or stdin; used in <<br>2. Standard output: Code 1, or stdout; used in 1>
3. Error Output: Code is 2, or stderr; used in 2>


[Test @test test]# ls-al > List.txt
Output the displayed results to the List.txt file, and replace the file if it exists!


[Test @test test]# ls-al >> list.txt
Add the displayed results to the List.txt file, the file is cumulative, the old data is retained!


[Test @test test]# ls-al 1> list.txt 2> list.err
The data to be displayed, the correct output to the List.txt error data output to List.err


[Test @test test]# ls-al 1> list.txt 2> &1
Output the displayed data, whether correct or error, to List.txt! Error and correct file output to the same file, it must be written in the method above! cannot be written in any other format!

[Test @test test]# ls-al 1> list.txt 2>/dev/null
The data that is displayed, the correct output to the List.txt error, is discarded! /dev/null, can be said to be a black hole device. is empty, which is not saved.

1.4 Why to use command output redirection

• When the information on the screen output is important and we need to save it;
• Background execution in the program, do not want him to interfere with the normal output of the screen when the results;
• The execution results of routine commands for some systems, such as those written in/etc/crontab, in the hope that he can be saved;
• Some execution orders, we already know his possible error message, so want to "2>/dev/null" to throw him away;
• When the error message and the correct message need to be output separately.

2 Pipeline Command (pipe)

As I said earlier, when the Bash command executes, the output data will appear, so if this group of data has to go through a few formalities to get the format we want, how should we set it? This involves a problem with the pipeline command (pipe), and the pipeline command uses the "| 』。


Example: Simple Pipeline command
Suppose we want to read the last command, what is the "number" of the root login?
So the steps that I take are:
1. Execute last and take all the people logged in this month to the data;
2. Use grep to extract the root of the above output data (stdout), and not others;
3. Use WC This can count the number of rows of the command to calculate the number of rows in the previous step of the data!
Since the last output is a one-time login, so as long as the calculation of a few lines on behalf of the login several times, through the above three steps, the last data gradually filtered, you can get our data! The entire command can be written as follows: [Test @test bin]# Last | grep root | Wc-l
This Pipeline command "| "The ability to handle only the correct information from the previous command, which is standard output (STDOUT), is not directly handled for Stdandard error."

2.1 Basic Pipeline Command Instruction introduction

Cut
Syntax: [Root @test/root]# cut-d "separator character" [-CF] Fields
Parameter description:
-D: followed by the character to be delimited, the preset is "whitespace"
-C: followed by "Number of characters"
-F: What is the number of blocks followed?
Example: [Root @test/root]# cat/etc/passwd | Cut-d ":"-F 1
Will passwd this file inside each line: used as a separator number, and list the first chunk! That's where the name is!
[Root @test/root]# Last | Cut-c1-20
Remove the last data, 1-20 characters from each line!
sort
Syntax: [root @test/root]# sort [-t delimiter] [(+ Start) (-end)] [-NRU]
Parameter description:
-T separators: use separators to separate different intervals, and the default is tab
+start-end: Sorted by start range to end interval
-N: Sort with "pure number" (otherwise it will be sorted in text form)
-R: Reverse Sort
-U: The same line that appears, listed only once!
Example: [Root @test/root]# cat/etc/passwd | Sort will list the individual accounts listed!
[Root @test/root]# cat/etc/passwd | SORT-T: +2N will be in the personal account, with the user ID to sort (by: to separate, the third is the ID, but the first code is 0)
[Root @test/root]# cat/etc/passwd | SORT-T: +2nr reversed-phase sort!
WC
Syntax: [Root @test/root]# WC [-LMW]
Parameter description:
-L: How many lines
-M: How many characters
-W: how many words
Example: [Root @test/root]# cat/etc/passwd | Wc-l How many lines are there in this file?
[Root @test/root]# cat/etc/passwd | Wc-w How many words are in this file!?
uniq This command is used to delete "duplicate rows out of only one"
Syntax: [Root @test/root]# uniq
Example: [Root @test/root]# Last | Cut-d ""-f1 | Sort | Uniq
Tee Command redirects to a file while displaying data on the screen
Syntax: [Root @test/root]# Last | Tee Last.list | Cut-d ""-f1
Example: [Root @test/root]# Last | Tee Last.list | Cut-d ""-f1
TR
Syntax: [Root @test/root]# tr [-ds] SET1
Parameter description:
-D: Delete the SET1 string
-S: Replace duplicate characters!
Example: [Root @test/root]# Last | TR ' [A-z] ' [A-z] ' <== lowercase to uppercase
[Root @test/root]# cat/etc/passwd | Tr-d: <==: This symbol is missing in/etc/passwd!
[Root @test/root]# Cat/home/test/dostxt | Tr-d ' \ r ' > Dostxt-nom
Split
Syntax: [Root @test/root]# split [-BL] input file output file leading character
Parameter description:
-B: Divide by file size
-L: divided by the number of rows
Example: [Root @test/root]# split-l 5/etc/passwd test <== will produce Testaa, Testab, Testac ... Wait for the file
Description: It's a lot easier under Linux! If you want to split the file, then use the-B size to limit the size of a segmented file, and if it is the number of rows, then use the-L line to split it!
Pipeline commands are quite important in bash's continuous handlers! In addition, the analysis of log file is also a very important link.
The standard input for piping to a command can be controlled more carefully using the standard input parameter "-". Example of a cat command
Eg:sort MyList | More
Sort MyList | Cat–n | Lpr
PWD | Cat–mylist | Lpr

Excerpt from: http://blog.sina.com.cn/s/blog_439f80c40101g2hr.html

Linux redirection related (reprint post, for yourself Cookbook)

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.