In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "how to redirect Linux". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Detailed explanation and application example of Icano redirection
1. Basic concepts (this is the premise of understanding the following knowledge, please be sure to understand it)
A, I FD O redirection is usually related to FD, and the number of FD of shell is usually 10, that is, zero nine.
B. There are 3 commonly used FD, which are 0 (stdin, standard input), 1 (stdout, standard output) and 2 (stderr, standard error output). By default, they are related to keyboard, monitor and monitor.
C, use
< 来改变读进的数据信道(stdin),使之从指定的档案读进; d、 用 >To change the outgoing data channel (stdout, stderr) to output to the specified file
E, 0 is
< 的默认值,因此 < 与 0 与 1>It's the same.
F. In IO redirection, the pipeline between stdout and stderr will be ready before the data is read from stdin.
G, pipe "|" (pipe line): the stdout of the previous command receives the stdin of the next command
The h and tee commands are to copy a copy of the stdout to the file without affecting the original Imax O.
I. Bash (ksh) the process of executing a command: analyze command-variable evaluation-command substitution (``and $())-redirect-wildcard expansion-determine path-execute command
J, () put command group into sub-shell to execute, also known as nested sub-shell, which has one very important feature: inheriting the Standard input, output, and error plus any other open file descriptors of the parent shell.
K, exec commands: commonly used to replace the current shell and restart a shell, in other words, there is no promoter shell. Any existing environment will be cleared when using this command. When exec operates on file descriptors, and only then, exec will not overwrite your current shell environment.
2. Basic IO
Cmd > file redirects stdout to the file file
Cmd > > file redirects stdout to the file file (append)
Cmd 1 > fiel redirects stdout to the file file
Cmd > file 2 > & 1 redirect stdout and stderr to the file file together
Cmd 2 > file redirects stderr to the file file
Cmd 2 > > file redirects stderr to file file (append)
Cmd > > file 2 > & 1 redirect stderr and stderr together to the file file (append)
Cmd
< file >The file2 cmd command takes file file as stdin and file2 file as stdout
Cat file opens file in read and write mode
Cmd
< file cmd 命令以 file 文件作为 stdin; cmd &n 使用系统调用 dup (2) 复制文件描述符 n 并把结果用作标准输出; file 运行一个命令并把错误输出(文件描述符 2)定向到 file。 ... 2>& 1 runs a command and merges its standard output and output. (strictly speaking, file descriptor 2 is created by copying file descriptor 1, but the effect is usually to merge two streams.)
Let's elaborate on 2 > & 1: 2 > & 1, that is, FD2=FD1, this does not mean that the value of FD2 is equal to the value of FD1, because > is to change the data channel sent, that is, to change the "data output channel" of FD2 to the "data output channel" of FD1. If that's all, this change doesn't seem to work, because the default output of FD2 and the default output of FD1 are originally monitor, the same! However, this has a special purpose when FD1 is other files, or even other FD. Please be sure to understand this.
Exec 0exec 1 > outfilename # Open the file outfilename as stdout.
Exec 2 > errfilename # Open the file errfilename as stderr.
Exec 0neighbors-# closes FD1.
Exec 5 > &-# close FD5.
Q: what are the consequences of shutting down FD0, FD1, FD2? What is the difference between restoring FD0, FD1, FD2 and closing FD0, FD1, FD2? What are the codes? After opening the FD3~FD9, after we have finished using it, do you think we should shut them down or restore them?
Here are some tips (for example, come from a CU post, forget the source, and make it up tomorrow):
Exec 6 > & 22 > ver
Command > > dev/null &
Exec 2 > & 6 # restore FD2
4. Simple examples
A, stdout and stderr are all sent to egrep through the pipeline:
(ls you no 2 > & 1 * ls yes 2 > & 1) 2 > & 1 | egrep\ * > file
(ls you no 2 > & 1 * ls yes 2 > & 1) | egrep\ * > file
(ls you no;ls yes) 2 > & 1 | egrep\ * > file
What we should pay attention to in this example is:
Understand the command execution order and pipeline "|": before the command execution, the redirection should be processed, and the stdout of nested sub-shell will be connected to the stdin of the egrep command. Nested sub-shell, with the two commands in () plus (), can be regarded as a command. Its FD1 has been connected to "|" and sent to egrep. When it encounters 2 > & 1, that is, FD2=FD1, that is, FD2, like FD1, is sent to the pipeline "|".
B. Nothing is sent to egrep through the pipeline, all to monitor. (ls you no 2 > & 1 * ls yes 2 > & 1) > & 2 | egrep\ * > file. Although FD2 is transferred to FD1 in (), outside (), > & 2 is encountered, and as a result, all are sent to monitor. Please understand:
(ls you no 2 > & 1) 1 > & 2 | egrep\ * > file # # send to monitor
Ls you no 2 > & 1 > & 2 | egrep\ * > file # # send it to the pipeline "|"
Ls you no 1 > & 22 > & 1 | egrep\ * > file # # send to monitor
5. Middle-level examples
Condition: stderr is sent to egrep via pipeline, and the correct message is still sent to monitor (unchanged)
Exec 4 > & 1; (ls you no 2 > & 11 > & 44 > &; ls yes 2 > & 11 >
& 4 4 > & -) | egrep\ * > file;exec 4 > &-
Or
Exec 4 > & 1; (ls you no;ls yes) 2 > & 1 >
& 4 4 > &-| egrep\ * > file;exec 4 > &-
If you add two conditions:
(1) cmd1 and cmd2 are required to run in parallel.
(2) assign the return value of cmd1 to the variable ss.
Is as follows:
Exec 3 > & 1 * * exec 4 > & 1
Ss=$ (ls you no 2 > & 1 1 > & 3 > &; echo $? > & 4) | egrep\ * > file) 4 > & 1)
Exec 3 > &; exec 4 > &-
Description:
Exec 3 > & 1TX 4 > & 1 establish FD3 to restore the FD1 in the following ls statement (sub-shell) to normal FD1, that is, output to monitor, you can think of FD3 as the original hard disk backup of FD1 (that is, output to monitor); create FD4, which will be used to save the return value of ls (echo $?), you can think of FD4 as storing the calculation "echo $?" during exams. Draft paper
(ls you no 2 > & 1 1 > & 3 3 > &; echo $? > & 4) remember the sub-shell and pipes mentioned earlier. This command first inherits FD0, FD1, FD2, FD3, FD4, which is located in front of the pipe, so connect the sub-shell's own FD1 to the pipe "|" before running the command. But our condition is that stderr is piped to egrep,stdout and still output to monitor. So through 2 > & 1, first send the pipe of the FD1 of the sub-shell to the FD2, so the stderr in the sub-shell is sent to the pipe "|"; then through 1 > & 3, restore the previous "hard disk backup" to the FD1 of the sub-shell, so the FD1 in the sub-shell is sent to the monitor. Then close 3 through 3 > & -, then run echo $?, whose output value is supposed to be sent to the pipeline, and send the output to the draft paper FD4 through > & 4, leaving it for backup.
((ls you no 2 > & 1 1 > & 3 3 > &; echo $? > & 4) | egrep\ * > file) so stderr piped to egrep, stdout to monitor, but also FD4, where did it go? (ls you no 2 > & 11 > & 3 > &; echo $? > & 4) | egrep\ * > file) 4 > & 1) the last 4 > & 1 redirects FD4 to FD1. But because its output is in $(), its value is assigned to the variable ss. The last line closes FD3 and FD4.
6. High-level examples
Command cmd1, cmd2, cmd3, cmd4. How to use one-way pipes to perform the following functions:
1. All commands are executed in parallel.
2. Cmd1 and cmd2 do not need stdin.
3. Stdout of cmd1 and cmd2 is directed to stdin of cmd3.
4. Stderr of cmd1 and cmd2 is directed to stdin of cmd4.
5. The stdout of cmd3 is directed to file a, and stderr is directed to the screen.
6. The stdout of cmd4 is directed to file b, and stderr is directed to the screen.
7. The return code of cmd1 is assigned to the variable s.
8. Temporary files cannot be used.
Solution:
Exec 3 > & 1; exec 4 > & 1
Cmd2 $(cmd1 1 > & 3; echo $? > & 4) | cmd2) 3 >
& 1 | cmd3 > a 2 > & 3) 2 > & 1 | cmd4 > b) 4 > & 1)
Exec 3 > &; exec 4 > &-
I explain this step by step (it's so complicated that I feel like I understand it. I'll look at it later. My brain is still blank for a few minutes. I didn't think I could read it. Exec 3 > & 1; exec 4 > & 1 is explained in the previous examples, that is, to establish FD3, restore its FD1 use to cmd1 and restore its FD2 use to cmd3, establish FD4, and save "echo $?" Output value of "draft paper".
The first pair of parentheses: (cmd1 1 > & 3; echo $? > & 4) and the following (first) pipe. In the first parenthesis (subshell), its FD1 is already connected to the pipe, so use FD3 to restore the FD1 to normal and prevent him from running into the pipe; the cmd1 here does not have stdin, and then save the error code that cmd1 runs into FD4.
The second pair of parentheses: ((cmd1 1 > & 3; echo $? > & 4) | cmd2) 3 > & 1 and then (second) pipe. The previous FD1 is no longer sent to cmd2, and FD2 does not send it by default, so cmd2 does not have stdin, so in the second pair of parentheses: stdout and stderr of cmd1 and cmd2 are the default output, and you have encountered "3 > & 1" all the time. Please note: "3 > & 1", first see a command in the second pair of parentheses. When they encounter the second pipe, the FD1 is connected to the pipe "|". Because of the function of "3 > & 1", the FD1 of the child shell is sent to FD3, so all the output of FD3 "flows" to cmd3, and because of inheritance (inheriting the command of the first line), FD3 is actually the stdout of cmd1 and cmd2, so "the stdout of cmd1 and cmd2 is directed to the stdin of cmd3".
The third pair of parentheses: ((cmd1 1 > & 3; echo $? > & 4) | cmd2) 3 > & 1 | cmd3 > a 2 > & 3) 2 > & 1 and the third pipe after it. The stdout of cmd1 and cmd2 has been directed to cmd3's stdin, and after processing, cmd3 > a means sending its stdout to file a. 2 > & 3 means that the error output of restoring cmd3 is FD3, which is sent to monitor. So "cmd3's stdout is directed to file a, and stderr is directed to the screen." If there is no "2 > & 3", then the error output of cmd3 will interfere with the error output of cmd1 and cmd2, so it is necessary! Notice the "2 > & 1" after the third pair of parentheses |, the FD1 of the child shell is originally connected to the pipe "|", but the child shell FD1 is generous and gives it to FD2, so the FD2 is connected to the pipe. Remember cmd1 and cmd2 before? Their stderr hasn't moved. So here, the fourth command, cmd4, is sent through the pipeline. That is, "stderr of cmd1 and cmd2 is directed to stdin of cmd4". It's easier later. Cmd4 > b means "stdout of cmd4 is directed to file b, stderr is directed to screen (default)"
The fourth pair of parentheses: (cmd1 1 > & 3; echo $? > & 4) | cmd2) 3 > & 1 | cmd3 > a 2 > & 3) 2 > & 1 | cmd4 > b) followed by 4 > & 1. The FD1 and FD2 in the four pairs of parentheses have been dealt with. But remember the previous "echo $? > & 4" piece of "draft paper"? the purpose of "4 > & 1" is to "send the contents of the draft paper to monitor", but because there is a $() on the outside to "wrap" it. Its value is then assigned to the variable "s".
That's all for "how to redirect Linux". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.