Windows command line input and output redirection issue

Rub network relatively recent school, DNS always on the lips, blocking every outlet bandwidth, the bandwidth of the total exports NAT has only 4MB / s (Source: 360 speed), alas, not a bunch of people to experience the devil knows what it feels like to share this bandwidth .

Ado, in Unix redirect with a feeling very high, nslookup now want to win under the error message "*** Can not find server name for address 10.3.9.5: Non-existent domain" exported with to txt file, search the next, ah, then there is this article.

And then tell the nonsense, windows This is blatant plagiarism ah, stdio (0), stdout (1), stderr (2) related to unix is ​​the same, I was speechless for Bill Gates, Ma king of the cottage with him than nothing Well ah

=========================== introduction of text ==================== =====

Redirection symbols are:>, >>, <,>, &, <& and |, only the first five of the following description, the last one is pipe, also fully consistent with Unix.

 

Section

    Start with a classic question, "1> nul 2> nul" means not only shielding normal output and a muting error output, then we immediately know 1 here indicates normal output (so-called "standard output" - -stdout), 2 represents the output error (i.e., so-called "standard error" --stderr).

    1 and 2 actually handles stdout and stderr digital code, as to what is the handle, I think it can be understood as a way to identify something, or that the handle points to a thing. For example, the "standard output" stdout to identify to handle, or handles directed stdout "standard output."

    There is also a handle --stdin, it is the so-called "standard input" logo, digital code is 0. 3 to 9 can be used in addition to, but does not define them.

 

    "Stdout" and "stderr" The default is to output to the console con (ie cmd window), and the "standard input" by default console con (ie keyboard) input, so the purpose is to redirect redirect the input and output streams from the default position to a new position. The symbol ">" and ">>" is a default handler code, and "<" default handler code is 0.

 

    "Echo hhhhhh" statement similar can be said that we are all too familiar, but this is just kind of the default state, in fact, there is also some content. Complete this sentence should be like this: "echo hhhhhh 1> con 2> con", meaning that the results echo command in the standard output and standard error output to the console in the con, but this time is the standard error output empty.

 

    Look at an example, if a dir give the wrong parameters, such as "dir / mm", then wrote a whole is "dir / mm 1> con 2> con", but this time the standard output is empty. If you write the words "dir / mm 1> hero.txt", it will print an error message on the screen but will not have content hero.txt in.

 

    Examples of a standard input again, "set / p var =" This fact is "set / p var = 0 <con", just because 0 <con is the default value may be omitted. Of course, we can read the input from a file, such as "set / p var = 0 <file.txt", 0 is the default value may be omitted.

 

    It represents nul "null device" is a device does not exist, the output air stream redirected to shut off device as equivalent. If the device to read input from the air, the natural thing is not read, but he is inputted, which is "set / p var = <nul" in "<nul" corresponds to the transport reasons, but not wrap .

 

Section II

    The following is a talk about the "redirect" between handler code. Not mentioned before there are 3 to 9 this seven digit code handles it, what is the use? To tell the truth, basically useless, it is recommended that if you are not eager to know this part, then do not look down.

 

   "Echo hero 1> hero.txt" This is a standard output redirected to a file hero.txt, point 1 corresponds to the handle of a code becomes con hero.txt. "Echo hero 3> hero.txt 1 <& 3", the result is generated phrase file hero.txt, which reads hero, the process is such that: "3> hero.txt" is a numeric codes directed handle 3 "Kong" to hero.txt; "1 <& 3" is to copy the handle numeric codes to point 3 to point 1, a point at this time is on hero.txt, so the standard output is redirected to a hero in a .txt.

 

    "I <& j" and "i> & j" effect is copied to the point j i. "Echo hero> hero.txt 2> & 1" means that the phrase, either standard or standard error output will be redirected to the hero.txt specific process: 1 by con directed into hero.txt, " 2> & 1 "are copied to the two points 1, 2 at this time point also becomes a hero.txt, 1 and 2 and therefore will be redirected to the hero.txt. Note: 1 is the symbol ">" default handle digital code.

 

    Look at the "echo hero 3> hero.txt", why this result can not be redirected to a file in it? Remember, we only want to redirect the standard input, standard output and standard error output, so the real "work" of only 0, 1 and 2, respectively, as they refer to the previous three, but did not refer to 3 any handler can only be used as an indirect amount.

 

    "More 3 <hero.txt 0> & 3" is a phrase file hero.txt, specific process: "3 <hero.txt" becomes the point 3 hero.txt, "0> & 3" to the copy point 3 to 0, 0 point to hero.txt (but this is to read data from hero.txt in). Emphasize that, really read data is 0 instead of 3,3 only as an intermediate amount only. Of course, we just could write this sentence: "more 0 <hero.txt" or direct "more <hero.txt".

 

    One more example of "echo hero 5> hero.txt 4> & 5 3 <& 4 1 <& 3", the result is output to the hero.txt. The specific process: point 5 becomes hero.txt, "4> & 5" copy point 5 to 4, "3> & 4" to point 4 of replication to 3, "1> & 3" to copy the point 3 to 1 , the final point is hero.txt 1, the output 1 of the referenced standard is redirected to hero.txt.

 

 

Section III

Note: The contents of this section revolves around the following example will be described.

 

Copy the contents to the clipboard

Code:

@echo off

echo hero is a good man! ! !

echo 1> nul 3> nul

echo hero

echo hero

echo how this is going, you can not do 1> con 4> con

pause

what on earth is it? Why results so unexpected?

    The problem here involves a so-called "backup", that is, before modifying the code to point to a handle, the system will handle the backup code to the original point up to now points to the first empty handle designator. The purpose of this program is that when the end of the line, the system can retrieve the original point to the backup.

 

   (Before you continue reading suggestions pen and paper ready, in order to record the changes in all code points, so as not confusing) 

 We now focus on the phrase "echo. 1> nul 3> nul" come up. Sentence exactly how does it work?

 

The first step: before running "1> nul", point 1 is the default value CON, code at this time point 3 to 9 are empty (initial value), the system will back up to point 31 (because 3 is the first empty symbol), 3 points to a con. That system point to the original copy 1 to 3, the purpose is the end of the statement can retrieve the original point, which is equivalent to the backup.

 

Step two: now point 3 is con. However, due to run "3> nul", therefore also backup point 3. 4 is empty at this time, the system routes the copied point con 3 to 4, i.e. 4 is now directed con. That is 3 to 4 as a backup.

 

The third step: Since "3> nul" such that 3 points to nul.

 

Step four: At the end of this line statement, 1 to retrieve the original point, we know from the above description, 3 1 backup, so 1 to restore "the original" pointing through 3, but at this point to have 3 becomes a nul, it points to a nul.

 

Fifth Step: 3 To restore the original point to find a point CON 4,4, so as to restore CON 3; 4 original point is empty, in which the backup 5, so that point 4 is returned to empty.

 

    At this point we rationalize it, now 1 point nul, 2 point to the default value con, 3 point con, are empty after 4 points. Then after running two echo statement since NUL 1 point, i.e., the standard output is redirected to an empty device, so that the display is shielded.

 

    Let's look at the phrase "echo how this is going, you can not do 1> con 4> con" The message is how does it work?

 

The first step: 1 current point is nul, due to run "1> con", so to back it up. 3 but this time point is con non-empty, it will be backed up to a point 4, i.e., 4 points nul.

 

Step Two: Also, because you want to run "4> con", it is now 4 points nul it has been backed up to 5, 5 future matter tentatively omitted.

 

The third step: After running "4> con" 4 points to con.

 

Step 4: After the end of the line program, 1 to 4 by pointing to recover. 4 con point, so that a point con thereby restoring the default state. 5,5 and 4 to find the point nul, point 4 so nul.

 

    Let us count the present case, 0 points con, 1 point con, 2 points con, 3 did not move or point con, 4 points nul, are empty after 5 points.

 

    Will a bit messy? Then it would be good to see a few times, or take a look at this, talking about some of the more fundamental: http: //hi.baidu.com/ wind _ Yin /blog/item/0b35a7db07615e47d1164ed8.html

 

Section IV

 

    We already know that "echo hero" is equivalent to "echo hero 1> con 2> con", con represents the console, can be seen as a special file con, this is what we can not establish a cause of con named file.

 

    Reanalysis of the ">" and ">>" redirection mechanism to be. When you want to redirect to a file hidden or system attributes, ">>" can operate normally, and ">" can not be operated. Accordingly I guess, for the ">" redirects the output, if the file does not exist of course, is to create a file, if the file exists on the first delete the file, then create a file that is not covered by the contents of the file but first delete files and then create a new file.

   

    By the third quarter of explanation to understand why you should like this statement "echo hero> nul> con> hero.txt> con" would prevail, right to the last one.

 

    The last point to note is that - can not be output to redirect output to a read-only file.

 

Finish.

Most of the redirection consistent with Unix or under WIN, ah


Author: chinajobs
Source: CSDN
Original: https: //blog.csdn.net/chinajobs/article/details/52277141
Copyright: This article is author original article, reproduced, please attach Bowen link!

Guess you like

Origin www.cnblogs.com/shawnchou/p/10929525.html