In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the example analysis of Linux Bash Shell command redirection, which is very detailed and has certain reference value. Friends who are interested must finish it!
Command redirection
Basically, this subtitle is a very important concept of Linux Bash Shell command redirection, so you have to take some thought here!
O what is redirection, redirect? ": to put it simply, just transfer your current information to another place! For example, if we transfer the current screen output data to the file, we can write: "ls-l / > test". The larger symbol ">" means to direct the output to the test file. At this time:
If you execute "ls-l /", the screen will list the files and directories of the root directory on the screen.
However, when you use > to direct to the test file, the screen will not display any information, but will output the results you just executed to the test file.
So, as long as you "vi test", you will know that the test file records the results of the information we just executed! However, it is important to note that when you use the > symbol to direct data from the screen to the file, then:
§if the file (that is, test) does not exist, the system will automatically set it up, but
When the file exists, then the system will first empty the contents of the file, and then write the data!
That is, if output to an existing file, hehe, that file will be overwritten!
In addition to the > symbol, there are three main input and output conditions during the execution of the bash command, which are:
1. Standard input; code is 0; or stdin; the method used is
< 2. 标准输出:代码为 1 ;或称为 stdout;使用的方式为 1>3. Error output: the code is 2; or the method used by stderr; is 2 >
Pay attention! There is no white space between 1 > and 2 >! The relevant instructions can be given as follows
[test @ test test] # ls-al > list.txt > list.txt list.txt 2 > list.err list.txt 2 > & 1 list.txt 2 > / dev/null with the trash can black hole device / dev/null to discard the data! This is very important!
Here we will explain several commonly used symbols and devices in command redirection:
O: output the correct data originally output from the screen to the file (file name) or device (device, such as printer) on the right side of >.
O > >: output the correct data originally output from the screen to the right side of > >. Unlike >, the file will not be overwritten, and the new data will be added to the * * side of the file in an "increased way".
O 2 >: output the error data that should have been output from the screen to the right of 2 >.
O / dev/null: it can be described as a black hole device!
Well, after we have a certain concept of ">, >", let's talk about the concept of "command output reorientation" in depth. As mentioned earlier, basically, the results of Linux execution can be roughly divided into "correct output" and "error output". For example, when you execute the find command in a general capacity, such as "find /-name testing", because you are a general identity and some folders are not allowed to enter by ordinary people, an error message will occur when you use find! But at the same time, if there is a testing file in the folder you can access, then the screen will also be output to show you! Therefore, there are two kinds of right and wrong output. (called Stdout and Stderror respectively) for example, the following is the execution result: the "find: / home/root: Permission denied" tells you that you do not have permission to enter the folder, this is the wrong output, then "/ home/test/tseting" is the correct output!
[test @ test test] # find /-name testing find: / home/test1: Permission denied is preset as 1 > for data! That 1 > is the output of correct data, and 2 > is the wrong data output item. In other words:
1 >: is to output the correct data to the designated place
2 >: is to output the wrong data to the designated place
Okay, so in the above example, how do we output the data to different places? It can be written as follows:
[test @ test test] # find /-name testing 1 > list_right 2 > list_error
In this way, in the results just executed, the lines of error messages with Permission will go to the list_error file, and the correct output data will be saved in the list_right file. Does that make sense? If there is any confusion, go and have a rest and have a look again!
Again, what if I just want the right information and I don't want the wrong information? Ha ha, this time / dev/null this trash can is very important! What is / dev/null? Basically, it's kind of like a "black hole" trash can function! When anything you enter is directed to this virtual trash can device, "he will disappear out of thin air." this thing is very useful! For example, in the above example, we can do this to throw away the wrong information!
[test @ test test] # find /-name testing 1 > list_right 2 > / dev/null
It's amazing! Error message will be gone! "heh heh! I'm so happy! Besides, what if I want to write all the data in the same file? The writing method needs to be written in a special way at this time, please pay attention to the writing below!
[test @ test test] # find /-name testing 1 > list 2 > list list 2 > & 1 & 1 is right!
OK! After learning about >, 2 >, > > and / dev/null, then what is that? He he! In the simplest terms, it means "read the data that originally needs to be entered by the keyboard through the file". The most obvious example is mail. Let's send a letter to root as root. We can do this:
1. Input data entirely from the keyboard: [root @ test test] # mail-s "test" root
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.