Standard I/O and piping
Standard inputs and outputs
The program is made up of instruction + data
There are three types of data flow in the program:
Input data flow: <--standard input (stdin), usually the default is the keyboard
Output data stream:--> standard output (stdout), general default to terminal window
Error output stream:--> standard error (STDERR), general default to terminal window
Standard input: 0
Standard output: 1
Error Output: 2
I/O redirection : Changing the default location
Output redirection: REDIRECT standard output (stdout) and standard error (STDERR) to the specified location
Format: command action symbol file name
Supported Operation symbols:
>: Redirect the standard output (stdout) to a file, overwriting the original file contents
>>: Redirect standard output (stdout) to file, append content based on original content
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/85/4A/wKioL1efLaHgaGkfAABY2uBPg18105.png "title=" Picture 1.png "alt=" Wkiol1eflahgagkfaaby2ubpg18105.png "/>
Description: Above is to say ll show the results output redirected to the file, when using >, file files are overwritten, when using >>, the original file and the redirected content are displayed, is the Append content
2>: Redirect the standard error (STDERR) to the file, overwriting the original content
2>>: Redirect standard error (stderr) to file, append content based on original content
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/85/4A/wKioL1efL83CVDWmAABesJJ4pRY120.png "title=" Picture 2.png "alt=" Wkiol1efl83cvdwmaabesjj4pry120.png "/>
Description: The error results of the command execution are displayed in the file files, when using 2>, overwriting the original file output, when using 2>>, the original file content and standard errors are displayed, is the Append content
&>: Redirects all output to a file, regardless of the error output to the specified location, overwriting the output
&>>: Redirect all output to a file, regardless of the error output to the specified location, append output
2>&1
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/85/4A/wKiom1efNWXw0BOwAADp-hR1RVA308.png "title=" Picture 3.png "alt=" Wkiom1efnwxw0bowaadp-hr1rva308.png "/>
Note: When using &> or &>>, when executing LL, is the standard output, LLS is the standard error, but no matter which output is redirected to the file,
2>&1 and &>,&>> the same, 2>&1 is used in the old version, to be used in the end, only effect, such as
Command >/path/toshmefile 2>&1
Command>>/path/to/somefile 2>&1
Understand: A and B, the output of A to B, if yes, the normal output to B, if the error, the content or output on B
SET-C: Prevents overwriting of existing files, but can append mandatory overrides: >|
Set +c: Allow overwrite
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/85/4B/wKiom1efOBriFNPBAABedYQk_NY605.png "title=" Picture 4.png "alt=" Wkiom1efobrifnpbaabedyqk_ny605.png "/>
Merge standard output from multiple programs
(command; command) >file
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/85/4B/wKiom1efOLDTAOtEAAAV1eA3NlU942.png "title=" Picture 5.png "alt=" Wkiom1efoldtaoteaaav1ea3nlu942.png "/>
Note: Redirect the standard output of ll and Echo Sha to the file files, so that this method can only be used for standard output,
Standard error cannot be used
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/85/4B/wKiom1efOVqRryZUAAAyDFPUNtE539.png "title=" Figure 6.png "alt=" Wkiom1efovqrryzuaaaydfpunte539.png "/>
TR: converting and deleting characters
TR [option]: SET1 [set2]==>[] not required
Options:
-C: Take the complement of the character ==> replace all characters that are not part of the first character set
-D: Delete all characters belonging to the first character set
-S: Represents a character that repeats repeatedly in a single character
-T: Converts the first character set corresponding character to the character corresponding to the second character set
Some uses:
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/85/4B/wKioL1efQa6T59kcAABHr8wLr6A876.png "title=" Picture 5.png "alt=" Wkiol1efqa6t59kcaabhr8wlr6a876.png "/>
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/85/4B/wKioL1efRJWyt5hqAAA-uwMak3I966.png "title=" Picture 6.png "alt=" Wkiol1efrjwyt5hqaaa-uwmak3i966.png "/>
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/85/4B/wKioL1efRe7j09s7AAAftz_EseI185.png "title=" Picture 7.png "alt=" Wkiol1efre7j09s7aaaftz_esei185.png "/>
As with the above effect, do not make special instructions
Pipeline:
Pipe Use symbols | To connect commands
Command1 |command2 |command3 ...
1. Send the standard output of command 1 to command 2, as standard input for command 2, and the standard output of command 2 to Command3 as standard input for command3 ....
2. Standard errors cannot be forwarded by pipeline by default, but can be implemented using 2>&1 or |&
3. The last command executes in the child shell process of the current shell process
4. Multiple tools can be combined
For example:
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/85/4C/wKioL1efSdbiLicYAABauvlTvwA454.png "title=" Picture 7.png "alt=" Wkiol1efsdbilicyaabauvltvwa454.png "/>650) this.width=650; src=" Http://s5.51cto.com/wyfs02/M00/ 85/4c/wkiol1efswoywicaaaa5i_ot7iq494.png "title=" image 8.png "alt=" Wkiol1efswoywicaaaa5i_ot7iq494.png "/>
Tee command:
Command1 |tee FileName | Command2
Save the standard output of the Command1 in a file, and then pipe the input to the Command2
Features: 1. Save different stages of output
2. Troubleshooting Complex Pipelines
3. Simultaneously view and record the output
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/85/4C/wKioL1efTZih8rDsAABc2k_EWxc943.png "title=" Picture 9.png "alt=" Wkiol1eftzih8rdsaabc2k_ewxc943.png "/>
Practice:
1. Convert the contents of the/etc/issue file to uppercase and save to/tmp/issue.out
Instructions for use:
Cat/etc/issue |tr ' A-Z ' A-Z ' >/tmp/issue.outcat/etc/issuecat/tmp/issue.out
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/85/4C/wKioL1efTnzgWExMAABApcAr09Y032.png "title=" Picture 10.png "alt=" Wkiol1eftnzgwexmaabapcar09y032.png "/>
2. Convert the current system login user's information to uppercase and save to/tmp/who.out
Instructions for use:
Who |tr ' A-Z ' A-Z ' >/tmp/who.out
W.H.O.
Cat/tmp/who.out
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/85/4C/wKiom1efTwKyx1SGAAAvYia6tbk300.png "title=" Picture 10.png "alt=" Wkiom1eftwkyx1sgaaavyia6tbk300.png "/>
3, a Linux user to the root email, request the message titled "Help", the message body is as follows:
Hello, I am Username, the system version is here,please help me to
Check it, thanks!
Operating system Version Information
Instructions:
Echo-e "Hello zhengwen \ n ' uname-r '" |mail-s "help" [email protected]
650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M02/85/4C/wKiom1efUsXzJnNNAAEL_RCO5cI629.png-wh_500x0-wm_3 -wmp_4-s_3337124048.png "title=" image 10.png "alt=" Wkiom1efusxzjnnnaael_rco5ci629.png-wh_50 "/>
4, the/root/file list, displayed as a line, and the file name separated by a space
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/85/4C/wKiom1efVBuTTRW1AAA3Q2DYy0U664.png "title=" Picture 11.png "alt=" Wkiom1efvbuttrw1aaa3q2dyy0u664.png "/>
As you can see, in the file list, the file defaults to one row
So to display a line is to delete the swap line character "\ n"
Instructions:
LS |tr-d ' \ n '
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/85/4C/wKiom1efVRHRB8LBAAAU23u9mT0866.png "title=" Picture 11.png "alt=" Wkiom1efvrhrb8lbaaau23u9mt0866.png "/>
5. The contents of the File1 file are: "1 2 3 4 5 6 7 8 9 10"
Directive 1:
Echo 1 2 3 4 5 6 7 8 9 10|tr "' + ' |BC
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/85/4C/wKioL1efVjeQ4ZrLAAAIaMjcq9Q704.png "title=" Picture 11.png "alt=" Wkiol1efvjeq4zrlaaaiamjcq9q704.png "/>
Directive 2:
Echo $[' echo 1 2 3 4 5 6 7 8 9 10|tr ' + '
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/85/4C/wKiom1efVvaTLuJhAAAUczX7hLM635.png "title=" Picture 11.png "alt=" Wkiom1efvvatlujhaaauczx7hlm635.png "/>
6. Remove the ' ^m ' character from the Windows text file
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/85/4E/wKiom1ef5__Cy5bnAAAemh6vD5c683.png "title=" Picture 1.png "alt=" Wkiom1ef5__cy5bnaaaemh6vd5c683.png "/>
File Test.txt is copied over the Windows, the Cat command using-a can see the text of the ' ^m ' character, to remove the ' ^m ' TR command is not recognized ' ^m ', ' ^m ' converted to octal to 015, the output is appended to the Hah file, the result is as follows
So the instructions are:
Cat test.txt|tr-d ' \015 ' >hahcat-a hah
7, processing string "Xt.,l 1 jr#! $mn 2 c*/fe 3 uz 4", keep only the numbers and spaces
Idea: To keep numbers and spaces, you want to erase all but the numbers and spaces
echo "Xt.,l 1 jr#! $mn 2 c*/fe 3 uz 4" |tr-c-d ' 0-9 \ Echo ' xt.,l 1 jr#! $mn 2 c*/fe 3 uz 4 ' |tr-c-d ' 0-9 \ n '
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/85/4E/wKioL1ef7qjxmaG0AAA7gFryW8Y726.png "title=" Picture 1.png "alt=" wkiol1ef7qjxmag0aaa7gfryw8y726.png "/> This problem trap: Command! $: The last parameter of the previous command can be called, so it is possible to use the above only as a character reference, using a strong reference '
8. Display the path variable in a separate row for each directory
The directory in the path variable is separated by default with ":", to display a separate line, you need to replace ":" With a newline character "\ n"
echo $PATH |tr ': ' \ n ' independent line echo $PATH |tr ': ' ' space separated
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/85/4F/wKioL1ef7zugCrmhAAA6P7v3RFI769.png "title=" Picture 1.png "alt=" Wkiol1ef7zugcrmhaaa6p7v3rfi769.png "/>
9. Delete blank lines of the specified file
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/85/4F/wKiom1ef8beggWWsAAAdKcsjtYc992.png "title=" Picture 1.png "alt=" Wkiom1ef8beggwwsaaadkcsjtyc992.png "/>
10. Display each word (letter) in the file in a separate line with no blank lines
Cat/etc/init.d/functions |tr-cs ' [: Alpha:] ' \ n '
Replaces all characters except the letter with a newline character, the-s option represents the repeated characters in a single
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/85/4F/wKiom1ef8-TC_wR7AAAWZTtCaRo326.png "title=" Picture 1.png "alt=" Wkiom1ef8-tc_wr7aaawzttcaro326.png "/>
Standard I/O and piping