In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
The main content of this article is to explain "the detailed introduction of Linux I Pot O redirection knowledge points". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn the "Linux Imax O redirection knowledge points detailed introduction" bar!
There are always three "files" open by default in Linux, stdin (keyboard), stdout (screen), and stderr (error message output to the screen). These three files and other open files can be redirected. Redirection, in short, captures the output of a file, command, program, script, or block of code in a script, and then sends these outputs as input to another file, command, program, or script.
Each open file is assigned a file descriptor. The file descriptors for stdin, stdout, and stderr are 0, 1, and 2, respectively. In addition to these three files, file descriptors 3 to 9 are retained for other files that need to be opened. In some cases, it is useful to assign these additional file descriptors to stdin, stdout, or stderr as temporary copy links. They need to be restored to normal after a complex redirection and refresh.
Redirect
> file
Redirect stdout to a file. If the file does not exist, create it, otherwise it will be overwritten.
Create a file that contains a list of directory trees:
Ls-lR > dir-tree.list
Empty the file:
: > file
This is a > operation that turns the file file into an empty file (that is, size is 0). If the file does not exist, create a 0-length file (the same effect as touch). Is a placeholder that does not produce any output
You can also omit: placeholder:
> file
The effect is the same as the above: >, but some shell (such as bash) may not support this form.
> > file
Redirect stdout to a file. If the file does not exist, create it, and if it does, append it to the file.
Script.sh 1 > filename # redirect stdout to the file "filename". Script.sh 1 > > filename # redirect and append stdout to the file "filename". Script.sh 2 > filename # redirect stderr to the file "filename". Script.sh 2 > > filename # redirect and append stderr to the file "filename".
& > file
Redirect both stdout and stderr to a file:
Script.sh & > / dev/null
M > file
M is a file descriptor and defaults to 1 if it is not explicitly specified.
File is a file name. The file descriptor m is redirected to the file file.
Script.sh 2 > error.log
M > & n
M is a file descriptor and defaults to 1 if it is not explicitly specified.
N is another file descriptor.
Script.sh 2 > & 1
Redirect stderr to stdout. Send the output of the error message to the place where the standard output points.
Exec 6Filescript.sh > & 6
By default, the file descriptor 1 (stdout) through 6 is redirected. All the output passed to stdout is sent to 6.
< file 从文件中接受输入。与 >Commands are in pairs and are usually used in combination. 0
< file 或 < file,前面的标准输入 stdin 0 可以省略。 grep search-word < filename jfile 为了读写 file,把文件 file 打开,并且将文件描述符 j 分配给它。 如果文件 file 不存在,那么就创建它。如果文件描述符 j 没指定,那默认是标准输入 stdin 0 。 echo 1234567890 >File # write string to File. Exec 3File # # Open File and assign fd 3 to it. Read-n 4 & 3 # # write a decimal point. Exec 3 > &-# close fd 3. Cat File # = > 1234.67890
(note: the output of the above command is different from the original, and the reason is unknown.)
Pipeline
Pipes are similar to >, but are actually more generic. Useful when you want to concatenate commands, scripts, files, and programs.
Cat * .txt | sort | uniq > result-file
The above command sorts the output of all .txt files and removes duplicate lines. * Save the result to result-file.
You can combine input and output redirection and / or multiple instances of a pipe on the same line:
Command
< input-file >Output-file
Equivalent to:
< input-file command >Output-file
But this kind of writing is not standard, and some shell may not support it.
You can redirect multiple output streams to a single file:
Ls-yz > > command.log 2 > & 1
Put the result of the error option yz into the file command.log. Because stderr is redirected to this file, all error messages point there.
Note that the following example does not give the same result:
Ls-yz 2 > & 1 > > command.log
Outputs an error message, but does not write to the file. The output of the command, if any, is written to the file command.log.
If both stdout and stderr are redirected at the same time, different order of commands will lead to different results.
Close the file descriptor
Stdout-shuts down the shutdown.
The child process inherits the open file descriptor. That's why the pipes work. If you want to prevent the file descriptor from being inherited, you can turn it off.
Only reschedule the stderr to one pipe.
Exec 3 > & 1 # Save the "value" of the current stdout (pointing fd3 to the same destination of fd0) ls-l 2 > & 1 > & 3 > & | grep bad 3 > & # close fd3 # ^ # (but not close 'ls', normal output is not affected by grep) ls-l 2 > & 1 > & 3 | grep bad # so that the output is transferred to fd3 It will not be affected by grep ls badabc-l 2 > & 1 > & 3 | grep bad # stderr is output through fd1 and will be affected by grep exec 3 > &-# for the remaining scripts, close it
Using the file descriptor 5 can cause problems. When Bash creates a child process using exec, the child process inherits the file descriptor 5 (see Chet Ramey's archive e-mail: RE: File descriptor 5 is held open). Don't mess with this particular file descriptor 5.
At this point, I believe that you have a deeper understanding of the "Linux I Pot O redirection knowledge points detailed introduction", might as well come to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.