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 <
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 The personal account, 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] ' <== change 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
Redirection and piping in Linux