Data flow redirection [go]

Source: Internet
Author: User


  Data flow re-orientation

Data flow Redirection (redirect) in the literal sense, it seems that the "data is transmitted to him elsewhere" appearance? That's right. Data flow redirection is the data that should appear on the screen after a command is run and transmitted to other places, such as files or devices (such as printers)! This thing is important under the Linux text mode! Especially if we want to save some data, it's even more useful!

What is data flow redirection

What is data flow redirection? This has to be done by the result of the command! In general, if you are going to run a command, usually he will be like this:


Figure 5.1.1, the data transmission of the command running process

When we run a command, this command may be read into the data by the file, processed, and then output to the screen. The standard output and standard error output, respectively, represent both the "normal" and "Std" outputs, which are output to the screen by default. So what is standard output and standard error output?

    • Standard output and standard error output

To put it simply, standard output refers to "the correct information returned by the command to run," and the standard error output can be understood as "error message after command failed." For a simple example, our system defaults to/etc/crontab but no/etc/vbirdsay, and when the "Cat/etc/crontab/etc/vbirdsay" command is issued, CAT will:

    • Standard output: After reading the/etc/crontab, the contents of the file are displayed on the screen;
    • Standard error Output: Error message displayed on screen because/etc/vbirdsay cannot be found

Whether the right or wrong data is the default output to the screen, the screen is of course chaotic! Could it be possible to separate the two strands of data through some mechanism? Of course you can! That's the function of data flow redirection! Data flow redirection allows you to transfer standard output (STDOUT) to a different file or device, respectively, to other files or devices, and the special characters to be transferred, respectively, as follows:

    1. Standard input (stdin): code 0, using < or <<;
    2. Standard output (STDOUT): Code 1, using > or >>;
    3. Standard error Output (STDERR): Code 2, using 2> or 2>>;

To understand stdout and stderr, let's start with an example exercise:

ll/  <== has a new file created! -rw-r--r--1 root root 1089 Feb 6 17:00/root/rootfile

It's weird! How can the screen have no data at all? This is because the original "LL/" displayed data has been redirected to the ~/rootfile file! The name of the ~/rootfile can be taken as you please. If you give "cat ~/rootfile" then you can see the data that should be on the screen originally. If I release again: "Ll/home > ~/rootfile", what does the content of that ~/rootfile file become? He will become "only ll/home data"! Hey! The original "LL/" data is gone? Yes! Because the file was created in the following way:

    1. If the file (in this case, ~/rootfile) does not exist, the system will automatically create it, but
    2. When this file is present, the system will clear the contents of the file before writing the data!
    3. That is, if the > output to an existing file, the file will be overwritten!

So what if I want to accumulate the data and don't want to delete the old data? It's good to use two greater than (>>) symbols. In the example above, you should change to " ll/>> ~/rootfile". Thus, when (1) ~/rootfile does not exist the system will actively create this file, (2) If the file already exists, then the data will be added to the bottom of the file!

This is the correct data for standard output, what if it is the error data of standard error output? Then go through 2> and 2>>! The same is the feature that covers (2>) and accumulates (2>>)! We just talked about the stdout code is 1 and the stderr code is 2, so this 2> is easy to understand, and if there is only >, then the default code is 1! Other words:

    • 1>: The "correct data" is output to the specified file or device in a covered manner;
    • 1>>: The "correct data" is output to the specified file or device in an additive manner;
    • 2>: The "wrong data" is output to the specified file or device in an overridden manner;
    • 2>>: The "wrong data" is output to the specified file or device in an additive manner;

Note that there are no spaces between "1>>" and "2>>"! Ok! After some concepts let's talk about how this guy is going to apply! When you run  find  this command as a general identity, some error messages may be generated due to permissions issues. For example, when you run " find/-name testing ", you might produce information like "Find:/root:permission denied". For example, the following example:

  example two: Use General identity account to find out if the under/home is known as. bashrc file exists [[email protected] ~]# su-dmtsai <== assuming my system is known as Dmtsai's account [[email protected] ~]$ find/home-name. BASHRC  <== identity is Dmtsai Oh! Find:/home/lost+found:permission denied <== standard Errorfind:/home/alex:permission Denied <== standard errorfind:/home/arod:permission denied <= = Standard ERROR/HOME/DMTSAI/.BASHRC <== standard output     

Due to the existence of the account that we created before/home, the family directory of those accounts is of course not accessible to you! So there will be errors and the correct data. OK, so what if I want to output the data to the list file? What happens when you run the Find/home-name bashrc > list? Hehe, you will find that the list contains just the "correct" output data, as for the screen will still have the wrong information appear! Racking What do I need to do if I want to put the correct and wrong data into separate files?

Find/home-name. BASHRC > List_right 2> list_error

Note that there is no information on the screen at this time! Because of the results that have just run, the few lines of Permission error messages will go to the List_error file, and the correct output data will be stored in the List_right file! Do you understand that? If it's a bit confusing, take a break and take a look!

    • /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/home-name. BASHRC 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/home-name. BASHRC > List 2> list  find/home-name. BASHRC > List 2>&1     <== correct /c7>

The first line of error in the above table is due to the fact that the two-strand data is written to a file at the same time, and no special syntax is used, at which point 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 &>! Generally speaking, brother Bird is more accustomed to using 2>&1 grammar!

    • 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"!

Cat > Catfiletestingcat file testcat catfiletestingcat file 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? OK! As shown below:

194 Feb  6 18:29 catfile# Note that the size of these two files will be exactly the same! Almost like using a CP to replicate 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:

EOF  <== 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 that netizens often ask: " 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?

The judgment basis of the command operation:;, &&, | |

In some cases, a lot of the commands I want to run at one time, but don't want to run it again, what's the good of that? Basically you have two options, one is through the 13th chapter to introduce the shell script to write scripts to run, one is through the following introduction to enter multiple commands at once Oh!

    • CMD; CMD (continuous command release without regard to command affinity)

At some point, we want to be able to run multiple commands at once, such as when I want to run two sync sync to write to disk before shutdown the computer, so what can I do? Do this:

Sync Sync Shutdown-h now

Use semicolons in the middle of commands and commands (;) To separate, so that the command before the semicolon runs and then immediately runs the following command. This is really convenient ah ~ again, a different angle to think, in case I want to create a file under a directory, that is, if the directory exists, then I create this file, if not exist, it is even. This means that the two commands are relevant to each other, and whether the previous command runs successfully is related to whether the latter command will run! Then you have to use && or | | Hello

    • $? (Command callback value) with && or | |

As mentioned above, there is a dependency between the two commands, and the main judgment of this dependency lies in the correctness of the result of the previous command's operation. Remember we introduced the command callback value before this chapter! Hey! Yes, you're smart! It is through this callback value! Review again " if the previous command runs correctly, a $ = 0 value will be returned under Linux." So how do we use this callback value to determine if a subsequent command is going to run? This has to be done by "&&" and "| | "The help of the!" Note that there are no spaces between the two &! that | is the [shift]+[\] key result.

Command release situation Description
CMD1 && CMD2 1. If the CMD1 is running and running correctly ($?=0), start running CMD2.
2. If the CMD1 is completed and is error ($?≠0), the CMD2 does not run.
cmd1 | | Cmd2 1. If the CMD1 is running and running correctly ($?=0), the CMD2 does not run.
2. If the CMD1 is complete and error ($?≠0), start running CMD2.

The above cmd1 and CMD2 are orders. Well, back to what we just imagined, we wanted to: (1) First to determine if a directory exists, (2) to create a file under that directory if it exists. Since we have not yet described how to use the Judgment (test), here we use LS and the callback value to determine whether the directory exists! Let's take a look at this exercise below:

ll/tmp/abc-rw-r--r--1 root root 0 Feb 7 12:43 hehe

Did you see that? If/TMP/ABC does not exist, touch will not be run, and if/TMP/ABC exists, then touch will start running! Very good use it! However, we have to manually create the directory, nerve-racking ~ can not be automatically judged, if not the directory is given to create it? Let's take a look at the following example:

Rm-r/tmp/abc                <== the results appear! There is a mkdir

If you repeat "LS/TMP/ABC | | MKDIR/TMP/ABC "screen will not be repeated mkdir error! This is because/TMP/ABC already exists, so the subsequent mkdir will not proceed! Do you understand this? Well, let's talk about it again, if I want to create/tmp/abc/hehe this file, but I don't know if/tmp/abc exists, then what's the good? Try it out:

LS/TMP/ABC | | MKDIR/TMP/ABC && Touch/tmp/abc/hehe

The above example three will always create/tmp/abc/hehe! Whether or not/tmp/abc exist. So how does example three explain it? Since the commands under Linux are run from left to right, there are several examples of example three that we'll look at:

    • (1) If the/TMP/ABC does not exist so return $?≠0, then (2) because | | Have you encountered a $0 non-$? Therefore began to MKDIR/TMP/ABC, because MKDIR/TMP/ABC will be successful, so the return $?=0 (3) because && encountered $?=0 will run Touch/tmp/abc/hehe, eventually hehe was created;

    • (1) If the/TMP/ABC exists so $?=0, then (2) because | | Have you met $0? Will not proceed, at this time $?=0 continue to pass backward, therefore (3) because && encountered $?=0 began to create/tmp/abc/hehe! The final/tmp/abc/hehe is created.

The entire process is illustrated as follows:


Figure 5.2.1, the relationship in which commands run sequentially

The above figure shows two strands of data, the upper segment is the command behavior when there is no/tmp/abc, and the lower segment is the command behavior where/TMP/ABC exists. As mentioned above, the lower segment due to the existence of/TMP/ABC so $?=0, so that the middle of the mkdir will not run! And will $?=0 continue to pass back to follow the touch to use! You understand? At any moment you can take this picture as a signal! Let's think about the example below.

Example: The LS test/tmp/vbirding whether the existence, if there is the display "exist", if not present, then show "not exist"! A: This also involves the question of logical judgment, if there is a data display, if it does not exist to display other data, then I can do this:
ls/tmp/vbirding && echo "exist" | | echo "not exist"
that means, when the ls/tmp/vbirding run, if correct, run echo "exist", if there is a problem, run echo "not exist"! What will happen if I write the following conditions?
ls/tmp/vbirding | | echo "not exist" && echo "exist"
this is actually problematic, why? Introduction to the process of figure 5.2.1 we know that the command is running backwards, so in the example above, if/tmp/vbirding does not exist, he will do the following:
  1. If the ls/tmp/vbirding does not exist, return a value that is not 0;
  2. Next Pass | | Judgment, found that the previous command callback is not a value of 0, so the program began to run echo "not exist", and the echo "not exist" program can certainly run successfully, so will return a 0 value to the following command;
  3. After && 's judgment, gee! It's 0! So just start running echo "exist".
So Ah, hey! In the second example, there is not exist and exist at the same time! It's amazing.

After this example exercise, you should understand that because the command is run one after the other, so if you really want to use judgment, then this && and | | | Order can not be mistaken. In general, there are three assumptions, namely:

Command1 && Command2 | | Command3

And the order usually does not change, because generally speaking, command2 and Command3 will be able to put the command that can certainly run successfully, therefore, according to the logic analysis of the above example, you will know why to put it so very useful! And..... Exams are also very frequent test ~

Data flow redirection [go]

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.