1)/dev/null Garbage bin black hole device and special wording
Imagine if I knew the error message would occur, so I would ignore the error message and not display or store it? This time the black hole device/dev/null is very important! This/dev/null can eat any information that directs this device. Revise the example above:
Find 2>/dev/null/HOME/DMTSAI/.BASHRC <== only stdout will appear on the screen, stderr is discarded
And imagine if I was going to write the correct and wrong data to the same file? This time you have to use a special wording! We also use the following examples to illustrate:
Find 2> List <==find2>&1 <==Find /home-name. BASHRC &> list <== correct
the first line of the above table is wrong because since two strands of data are written to a file at the same time, and no special syntax is used, the two strands of data may cross-write to the file, resulting in a sequence of confusion . So although the final list file will still be generated, the data in the array will be weird, rather than the output sort on the original screen. As with the special syntax for writing the same file as shown in the table above, you can use the 2>&1 or &>!
2) Standard input: < and <<
After understanding the stderr and stdout, then what is that <? Oh! In the simplest terms, it means " replacing data that would otherwise need to be entered by the keyboard with the contents of the file". Let's start with the cat command below to see what is called "keyboard input"!
Catcat > catfiletestingcatfile test<== Press [Ctrl] here +cat catfiletestingcatfile test
since joining > after cat, the Catfile will be actively created, and the content is just the two lines of data entered on the keyboard. Well! Can I replace the keyboard input with a plain text file, that is, replace the keystrokes with the contents of a file? Yes, I can! As shown below:
Example Seven: a simple process of replacing keyboard input with stdin to create new files [email protected]~]#Cat> Catfile < ~/. Bashrc[[email protected]~]# ll Catfile ~/. BASHRC-rw-r--r--1Root root194Sep - -: $/root/. BASHRC-rw-r--r--1Root root194Feb6 -: incatfile# Note that the size of these two files will be identical! It's almost like usingCPTo copy the general!
It's a very helpful thing! This is especially true with the use of commands like mail. after understanding <, it is a terrible thing to come back << this two consecutive less than the symbol. He represents the "end of the input character" meaning! For example: "I'm going to use cat to directly output the input information to Catfile, and when the keyboard enters EOF, that input ends", I can do this:
Cat " EOF "> This is a test. > OK now stop> EOF <== Enter this keyword and immediately end without having to enter [ctrl]+cat catfilethis is a test. OK now stop <== only these two lines, there will be no keyword that line!
Did you see it? Using the control character on the << right, we can terminate the input at once without having to enter [Crtl]+d] to end the mile! This is very helpful for program writing. OK, so why use command output redirection? Let's talk about it!
- The information on the screen output is important, and we need to save it;
- Background running the program, do not want him to interfere with the normal output of the screen when the results;
- Some of the system's routine commands (such as files written in/etc/crontab) run the result, hoping that he can save it;
- Some of the possible known error messages running the command, when you want to "2>/dev/null" to throw him away;
- When the error message and the correct information need to be output separately.
Of course there are a lot of features, the simplest is often asked: " why my root will receive the system Crontab sent error message " This is a common mistake, and if we already know that this error message can be ignored, ah! "2> ErrorFile" This function is very important! Did you get it?
References: Http://vbird.dic.ksu.edu.tw/linux_basic/0320bash.php#redirect