Detailed introduction to the use of Linux redirection
Developer on Alibaba Coud: Build your first app with APIs, SDKs, and tutorials on the Alibaba Cloud. Read more ＞
To introduce in detail how to use Linux redirection refers to modifying some of the original default items and changing the default execution mode of the original system commands, for example, if I don't want to see the output on the monitor, but want to output it to a file, I can use Linux redirection to do this...
To introduce in detail how to use Linux redirection refers to modifying some of the original default items and changing the default execution mode of the original system commands, for example, I don't want to see the output on the monitor. Instead, I want to output the output to a file and use Linux redirection. In Linux, the default input is a keyboard, and the output is a display. You can use redirection to change these settings. For example, when you use wc commands, you must manually enter a text to calculate the number of characters. after redirection, you can directly point a written file to this command with '<, the number of characters in the file can be counted. The same is true for output. you can redirect the screen output to a file and view the result in the file. The redirection operator can be used to redirect command input and output data streams from the default location to other locations. the input or output data stream locations are called handles. There are three common handles. of course, the handles can be expanded on their own, general operating systems provide similar functions. Handle code handle description www.2cto.com STDIN 0 input STDOUT 1 output information to the prompt window STDERR 2 output error information to the prompt window default <redirect input operator is 0, the default> redirect output operator is 1. After you type the <or> operator, you must specify the data read/write location, which can be a file name or another existing handle. To specify the redirection to an existing handle, use the and & character followed by the handle number (that is, & handle number) to be redirected ). For example, the following command can redirect handle 2 (STDERR) to handle 1 (STDOUT): 2> & 1 the following table lists the operators that can be used to redirect input and output data streams: function description of the redirection operator in www.2cto.com Linux> write command output to a file or device, instead of a command prompt or handle <read command input from the file rather than from the keyboard or handle> add command output to the end of the file without deleting the existing information in the file> & output a handle write the input to another handle <& read the input from one handle and write it to another handle output | read the output from one command and write it into the input of another command; this is also called the pipeline operator. now let's look back at the above statement mysh> mylog.txt 2> & 1 to understand:> mylog.txtitalian is to redirect the standard output to mylog.txt, which is equivalent to mysh 1> mylog.txt; 2> & 1 means to redirect the error output to the handle 1 standard output. In the yshcommand execution process, both the standard output and incorrect output will be redirected to mylog.txt. the Function of redirecting www.2cto.com is very powerful. if you are interested, you can try different combinations to see what the results will be after and after the positions change? In some cases, we may not want to record any standard output or error output. you can use mysh> null 2> null or mysh>/dev/null 2>/dev/null; i/O redirection 1. basic concepts (this is the prerequisite for understanding the knowledge behind, please be sure to understand) a and I/O redirection are usually related to FD, shell FD is usually 10, that is, 0 ~ 9. B. There are three commonly used FD types: 0 (stdin, standard input), 1 (stdout, standard output), and 2 (stderr, standard error output ), by default, it is related to keyboard, monitor, and monitor. www.2cto.com c. use <to change the read-in Data Channel (stdin) so that it can be read from the specified file; d. Use> to change the data channel (stdout, stderr) sent to the specified file. e and 0 are the default values of <, so <and 0 <是一样的；同理，> It is the same as 1>; f. In IO redirection, the stdout and stderr pipelines will be prepared before reading data from stdin; g. pipeline "|" (pipe line): The stdout of the previous command receives stdin from the next command; h and tee commands do not affect the original I/O, copy stdout to the file. the process of executing commands in I and bash (ksh): analysis command-variable evaluate-Command substitution (''and $ ()) -redirect-wildcard expand-confirm path-execute command; j, () place command group in sub-shell for execution, also known as nested sub-shell, it inherits the Standard input, output, and error plus any other open file descriptors of the parent shell. K. exec command: it is often used to replace the current shell and restart a shell. In other words, there is no starting shell. When you use this command, any existing environment will be cleared. When exec operates on file descriptors, it is only in this case that exec will not overwrite your current shell environment. Www.2cto.com 2. basic IO cmd> file redirects stdout to the file; cmd> file redirects stdout to the file (append ); cmd 1> fiel redirects stdout to the file; cmd> file 2> & 1 redirects stdout and stderr together to the file; cmd 2> file redirects stderr to the file; cmd 2> file redirects stderr to the file (append ); cmd> file 2> & 1 redirects stderr and stderr together to the file (append). cmd <file> file2 cmd command uses the file as stdin, use a file2 file as stdout; cat <> file to open f in read/write mode Ile; cmd <file cmd command uses the file as stdin; cmd <delimiter Here document, which is read from stdin until the delimiter is encountered. 3. advanced IO www.2cto.com> & n use the system to call dup (2) copy the file descriptor n and use the result as the standard output. <& n standard input is copied from the file descriptor n; <&-disable the standard input (keyboard);> &-disable the standard output; n <&-indicates that the n input is disabled; n> &-indicates that the n output is disabled; all the above forms can lead to a number. in this case, the created file descriptor is specified by this number rather than the default 0 or 1. For example:... 2> run a command in file and direct the error output (file descriptor 2) to file.... 2> & 1 run a command and merge its standard output and output. (Strictly speaking, file descriptor 2 is created by copying file descriptor 1, but the effect is usually that two streams are merged .) Www.2cto.com: 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 say, change the "data output channel" of FD2 to "data output channel" of FD1 ". In this case, this change seems to have no effect, because the default output of FD2 and the default output of FD1 are both monitor, the same! However, when FD1 is another file or even another FD, this has a special purpose. Be sure to understand this. Exec 0 exec 1> outfilename # open the file outfilename as stdout. Exec 2> errfilename # open the file errfilename as stderr. Exec 0 <&-# Disable FD0. Exec 1> &-# Disable FD1. Exec 5> &-# Disable FD5. This completes the study of Linux redirection.
This article is an English version of an article which is originally in the Chinese language on aliyun.com and is provided for information purposes only. This website makes no representation or warranty of any kind, either expressed or implied, as to the accuracy, completeness ownership or
reliability of the article or any translations thereof. If you have any concerns or complaints relating to the article, please send an email, providing a detailed description of the concern or
complaint, to email@example.com. A staff member will contact you within 5 working days. Once verified, infringing content will be removed immediately.
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:
and provide relevant evidence. A staff member will contact you within 5 working days.