Linux Learning Materials-command redirection

Source: Internet
Author: User

command re-orientation
Basically, this sub-topic is a very important concept of bash, and it takes a little thought to get it done!
What is "re-orientation, redirect?" ": To put it simply, it's about transferring your current information to other places!" For example, we often use the current screen output data to go to the file, you can write: "Ls-l/> Test", that the greater than the symbol ">" is the output of the results to the test this file meaning! This time:
If you execute "ls-l/", the screen will list the files and directories of the root directory on the screen;
However, when using > To test this file, the screen will not display any messages, but will output the results you have just executed to the test file.
So, if you just "VI test", you'll know that the test file records the results of the data we just executed! However, it is very special to note that when you use the > symbol to direct the data from the screen to the file:
If the file is not present, the system will automatically set him up, but
When this file is present, the system will first empty the file and then write the data!
That is, if the > output to a file in the case, hehe, the files will be overwritten!
In addition to this > notation, there are three main output conditions in the execution of the Bash command, namely:
Standard input, code 0, or stdin; used in <
Standard output: Code 1, or stdout; used in 1>
Error Output: Code 2, or stderr; used in 2>
The basic instructions are written in the following ways:
Directive 1>
1>>
2>
2>>
< equipment or Archives

The left must be an instruction, and on the right it could be a device or a file! Watch out! There is no space between that 1> and 2>! The relevant instructions for use can be illustrated as follows:
[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, which is the cumulative, old data retention!
[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!
[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!
Attention! Error and correct file output to the same file, it must be written in the method above!
cannot be written in any other format!


This idea is quite important, especially when it comes to/etc/crontab, and if we know the wrong message and don't want the wrong message to fill the root mailbox, we have to 2> with/dev/null, the trash can, to throw the data out! This is quite important!

Here, let's illustrate the command re-orientation of several commonly used symbols and devices:
<: Read the parameter file from the right of <;
>: Output the correct data from the screen output to the file (filename) or device (device, such as printer) to the right of >;
>>: Output The correct data originally from the screen to >> to the right, unlike >, the file will not be overwritten, and the new data will be added to the last side of the file in "added way";
2>: Output error data that should have been output by the screen to the right of 2>.
/dev/null: Can be said to be a black hole device!
Well, for the ">, >>" These two things have a certain concept, we have to talk about the "command output re-guidance" concept it! As mentioned earlier, basically, the results of Linux execution can be roughly divided into "correct output" and "error output" two ways. For example, when you execute the Find command as a general identity, such as when you execute "Find/-name testing", because you are a general identity, and some folders do not allow the general identity of the person to enter, so, when you use Find, there will be an error message occurs! But at the same time if there is testing this file in the folder you can access, then the screen will be output to show you! So, there are two kinds of output that are correct and wrong! (called Stdout and Stderror, respectively) For example, the following is the result of the execution: "Find:/home/root:permission denied" tells you that the folder you do not have permission to enter, this is the wrong output, then "/home/test/ Tseting "is the right output!
[Test @test test]# Find/-name Testing
Find:/home/test1:permission denied <== This is the wrong output
Find:/home/root:permission denied <== This is the wrong output
Find:/home/masda:permission denied <== This is the wrong output
/home/test/testing <== This is the "right" output
[Test @test test]#

Okay, so what if we want to export the data to the list file? What happens when I execute Find/-name testing > list? Hehe, you will find that the list has just the "correct" output data, as for the screen will still have the wrong message appeared! Racking What do you need to do if you want to store the correct and incorrect data in separate files?! Oh! In fact, in the data re-orientation, the correct wording should be "1>" and "2>"! But if only > the default is to use 1> to do the data! The 1> is outputting the correct data, and 2> is the wrong data output item. Other words:

1>: The correct data is exported to the specified place
2>: is to output the wrong data to the specified place
OK, so in the example above, how do we export the data to different places? It can be written like this:
[Test @test test]# Find/-name testing 1> list_right 2> list_error

In this way, the results just executed, the Permission of the few lines of error messages will go to list_error this file, as for the correct output data will be stored in list_right this file! Do you understand that? If it's a bit confusing, take a break and take a look!!

Again, if I just want the right data, the wrong information I don't want it? Oh, this time/dev/null this rubbish bin is very important! What is/dev/null? Basically, it's kind of like a "black hole" garbage can function! When anything you enter is directed to this virtual dustbin device, "he will disappear out of thin air ~ ~", this thing is very useful! For example, in the example above, we can do this to throw out the wrong information!
[Test @test test]# Find/-name testing 1> list_right 2>/dev/null

It's amazing! Error message will be "gone!" Oh I'm so glad! Also, what if I want to write the data in the same file? This time writing needs to use a special wording, please pay attention to the bottom of the wording of yo!
[Test @test test]# Find/-name testing 1> list 2> list<== error notation
[Test @test tset]# Find/-name testing 1> list 2>&1 <== correct notation


Please pay special attention to this point! Write to the same file at the same time need to use 2>&1 to Yo!

Ok! Understand the 2>, >> and/dev/null, then that < is what Ah!? Oh! In the simplest terms, that means "to read data from a keyboard into a file," the most obvious example of this is mail! We'll send a letter to root as root, so we can do this:
1. Fully input data by keyboard:
[Root @test test]# mail-s "test" root <==-S for title, root for recipient
I am root! <== The following data are entered by the keyboard
That ' s OK
. <== you want to end the keyboard input, you need to add it to the front of the line. Can!
Cc. <== do I need a blind copy? If you don't need it, just press Enter!
EOF <== indicates the hint character sent out!
2. Replace the input with the file
[Test @test tset]# mail-s "test" root </ROOT/.BASHRC <== send. BASHRC content to root!

It's fun! ^_^ so you can send the letter out! So, familiar with the command to re-guide the words, you are quite helpful yo!

OK, so why use command output redirection? This question is bound to bother you a bit, if you've never written a script! Well, let's talk about it!
When the information on the screen output is important, and we need to save him;
Background execution in 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 (for example, files written in/etc/crontab) result in the execution of 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.
Of course there are a lot of features, the simplest is that netizens often ask: "Why my root will receive the system Crontab sent the error message" This is a common mistake, and if we already know that the error message can be ignored, ah! "2> ErrorFile" This function is very important! Did you get it??

Linux Learning Materials-command redirection

Related Article

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.