Inux 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 the inux redirection to a file, it means to modify some of the original default items and change 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
STDIN 0 keyboard input
STDOUT 1 output information to the prompt window
STDERR 2 output error information to the prompt window
The default <redirect input operator is 0, and 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:
Linux redirection operator function description
> Write command output to a file or device instead of a command prompt or handle
<Read command input from a file instead of a keyboard or handle
> Add the command output to the end of the file without deleting the existing information in the file.
> & Write the output of one handle to the input of another handle
<& Read input from one handle and write it into another handle output
| Read the output from one command and write it into the input of another command; also known as the pipeline operator
Now let's look back at the above statement mysh> mylog.txt 2> & 1 to understand:
> Mylog.txtis intended to redirect the standard output to mylog.txt, which is equivalent to mysh 1> mylog.txt;
2> & 1 running;
The redirection function is very powerful. if you are interested, you can try different combinations to see what results will happen when the front and back 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 details
1. basic concepts (this is the prerequisite for understanding the following knowledge. please be sure to understand it)
A, I/O redirection is usually related to FD, and 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 ), the default value is related to keyboard, monitor, and monitor;
C. use <to change the reading Data Channel (stdin) so that it can read from the specified file;
D. Use> to change the output data channel (stdout, stderr) and output it to the specified file;
E, 0 is the default value of <, so <and 0 <是一样的;同理,> It is the same as 1>;
F. In IO redirection, the stdout and stderr pipelines are prepared before reading data from stdin;
G. pipeline "|" (pipe line): stdout of the previous command receives stdin of the next command;
H. The tee command copies stdout to the file without affecting the original I/O;
I. bash (ksh) command execution process: analysis command-variable evaluate-Command substitution (''and $ ()-redirect-wildcard expand-confirm path-execute command;
J. () place the command group in sub-shell for execution, also known as nested sub-shell. it has a very important feature: inherit the Standard input, output, and, and error plus any other open file descriptors.
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.
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 redirect stderr and stderr together to the file (append );
Cmd <file> The file2 cmd command uses the file as stdin and the file2 file as stdout;
Cat <> file: open the file in read/write mode;
Cmd <file cmd command uses file as stdin;
Cmd <delimiter Here document, read from stdin until the delimiter is encountered.
3. advanced IO
> & N use the system to call dup (2) copy the file descriptor n and use the result as the standard output;
<& N standard input copied from file descriptor n;
<&-Disable the standard input (keyboard );
> &-Disable standard output;
N <&-indicates that n is disabled;
N> &-indicates that output n 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 .)
For details about 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 used to change the data channel sent, that is to say, change "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.
1 COMMAND_OUTPUT> 2 # redirect stdout to a file. 3 # if this file is not available, it will be created, otherwise it will be overwritten. 4 5 ls-lR> dir-tree.list 6 # Create a file that contains the directory tree list. 7 8:> filename 9 #> the file "filename" is truncated to 0 length. 10 # if the file does not exist, create a zero-length File (same effect as 'touch ). 11 #: it is a placeholder and does not produce any output. 12 13> filename 14 #> the file "filename" is truncated to 0. 15 # if the file does not exist, create a zero-length File (same effect as 'touch ). 16 # (same effect as ":>" above, but may not work in some shells .) 17 18 COMMAND_OUTPUT> 19 # redirect stdout to a file. 20 # if the file does not exist, create it. if yes, append it to the end of the file. 21 22 23 # Single-line redirection command (will only affect the row where they are located): 24 # -------------------------------------------------------------- 25 26 1> filename 27 # redirect stdout to the file ". 28 1> filename 29 # redirect and append stdout to the file "filename ". 30 2> filename 31 # redirect stderr to the file "filename ". 32 2> filename 33 # redirect and append stderr to the file "filename ". 34 &> filename 35 # redirect both stdout and stderr to the file "filename ". 36 37 #=================================================== ========================================================== = 38 # stdout redirection, one row at a time. 39 LOGFILE = script. log 40 41 echo "This statement is sent to the log file, \" $ LOGFILE \". "1> $ LOGFILE 42 echo" This statement is appended to \ "$ LOGFILE \". "1> $ LOGFILE 43 echo" This statement is also appended to \ "$ LOGFILE \". "1> $ LOGFILE 44 echo" This statement is echoed to stdout, and will not appear in \ "$ LOGFILE \". "45 # after each line, these redirection commands will automatically" reset ". 46 47 48 49 # redirect stderr, one row at a time. 50 ERRORFILE = script. errors 51 52 bad_command1 2> $ ERRORFILE # The error message is sent to $ ERRORFILE. 53 bad_command2 2> $ ERRORFILE # add the error message to $ ERRORFILE. 54 bad_command3 # The error message is echo to stderr, 55 # + and does not appear in $ ERRORFILE. 56 # after each line, these redirection commands will automatically "reset ". 57 #=================================================== ========================================================== 58 59 60 61 2> & 1 62 # redirect stderr to stdout. 63 # The error message is sent to a place like stdout. 64 65 I> & j 66 # redirect file descriptor I to j. 67 # All output pointing to the I file is sent to j. 68 69> & j 70 # default, redirection file descriptor 1 (stdout) to j. 71 # All output transmitted to stdout is sent to j. 72 73 0 <FILENAME 74 <FILENAME 75 # accept input from the file. 76 # It is a paired Command with ">" and is usually used in combination. 77 #78 # grep search-word
Filename 82 # to read and write "filename", open the file "filename" and assign the file descriptor "j" to it. 83 # if the file "filename" does not exist, create it. 84 # if the file descriptor "j" is not specified, the default value is fd 0 and stdin. 85 #86 # this type of application is usually used to write to a specified place in a file. 87 echo 1234567890> File # write the string to "File ". 88 exec 3 <> File # Open "File" and assign it fd 3. 89 read-n 4 <& 3 # read-only 4 characters. 90 echo-n.> & 3 # write a decimal point. 91 exec 3> &-# Disable fd 3. 92 cat File #=> 1234.67890 93 # random storage. 94 95 96 97 | 98 # MPs queue. 99 # General Purpose processing and command chain tools. 100 # similar to ">", but more common. 101 # It is useful for Concatenating commands, scripts, files, and programs. 102 cat *. txt | sort | uniq> result-file 103 # sort the output of all. txt files and delete duplicate rows. 104 # save the result to "result-file.
You can combine multiple instances of the input/output redirection and (or) pipeline to write them on one row.
1 command < input-file > output-file 2 3 command1 | command2 | command3 > output-file
You can redirect multiple output streams to one file.
1 ls-yz> command. log 2> & 1 2 # put the result of the incorrect option "yz" to the file "command. log. 3 # because stderr is redirected to this file, 4 # + all error messages will point to it. 5 6 # note that the following example will not give the same result. 7 ls-yz 2> & 1> command. log 8 # output an error message, but it is not written to the file. 9 10 # if both stdout and stderr are redirected, the order of the 11 # + command will be somewhat different.
Exec The command redirects stdin to the file. from this sentence, all the input is from this file, rather than the standard input (usually keyboard input ). this provides a method for reading files by row, and you can use sed and/or awk to analyze each row. UseExec redirection standard input
1 #! /Bin/bash 2 # use 'exec 'to redirect standard input. 3 4 5 exec 6 <& 0 # link file descriptor #6 with stdin. 6 # save stdin. 7 8 exec <data-file # stdin is replaced by the file "data-file. 9 10 read a1 # read the first line of the file "data-file. 11 read a2 # read the second line of the file "data-file. 12 13 echo 14 echo "Following lines read from file. "15 echo" ------------------------------- "16 echo $ a1 17 echo $ a2 18 19 echo; echo 20 21 exec 0 <& 6 6 <&-22 # Now restore stdin from fd #6, because we just redirected stdin to #6, 23 # + and then disable fd #6 (6 <&-) so that this descriptor can continue to be used by other processes. 24 #25 # <& 6 6 <&-you can do this. 26 27 echo-n "Enter data" 28 read b1 # Now "read" is back to normal, that is, reading from stdin. 29 echo "Input read from stdin. "30 echo" ---------------------- "31 echo" b1 = $ b1 "32 33 echo 34 35 exit 0
Use exec to redirect stdout
1 #! /Bin/bash 2 # reassign-stdout.sh 3 4 logfile.txt 5 6 exec 6> & 1 # connect fd #6 to stdout. 7 # save stdout. 8 9 exec> $ LOGFILE # stdout is replaced by the "logfile.txt" file. 10 11 # --------------------------------------------------------- #12 # The output of all commands in this section is sent to the file $ LOGFILE. 13 14 echo-n "Logfile: "15 date 16 echo" ----------------------------------- "17 echo 18 19 echo" Output of \ "ls-al \" command "20 echo 21 ls-al 22 echo; echo 23 echo "Output of \" df \ "command" 24 echo 25 df 26 27 # --------------------------------------------------------- #28 29 exec 1> & 6> &-# restore stdout, disable the file descriptor #6. 30 31 echo 32 echo "= stdout now restored to default =" 33 echo 34 ls-al 35 echo 36 37 exit 0 |
Use exec to redirect stdin and stdout in the same script
1 #! /Bin/bash 2 # upperconv. sh 3 # Convert a specified input file to uppercase. 4 5 E_FILE_ACCESS = 70 6 E_WRONG_ARGS = 71 7 8 if [! -R "$1"] # determines whether the specified input file is readable? 9 then 10 echo "Can't read from input file! "11 echo" Usage: $0 input-file output-file "12 exit $ E_FILE_ACCESS 13 fi # even if the input file ($1) if 14 # + is not specified, it will still exit with the same error (why ?). 15 16 if [-z "$2"] 17 then 18 echo "Need to specify output file. "19 echo" Usage: $0 input-file output-file "20 exit $ E_WRONG_ARGS 21 fi 22 23 24 exec 4 <& 0 25 exec <$1 # will be read from the input file. 26 27 exec 7> & 1 28 exec> $2 # write it to the output file. 29 # assume that the output file is writable (add check ?). 30 31 # --------------------------------------------- 32 cat-| tr a-z A-Z # Convert to uppercase. 33 # ^ # read from stdin. reads from stdin. 34 # ^ # write to stdout. 35 # however, stdin and stdout are all redirected. 36 # --------------------------------------------- 37 38 exec 1> & 7 7> &-# restore stout. 39 exec 0 <& 4 4 <&-# restore stdin. 40 41 # After recovery, the following line of code will be printed to stdout as expected. 42 echo "File \" $1 \ "written to \" $2 \ "as uppercase conversion. "43 44 exit 0 |
I/O redirection is a way to avoid the terrible issue of unaccessable variables in sub-shells.
Avoid sub-shell
1 #! /Bin/bash 2 # avoid-subshell.sh 3 # advice from Matthew Walker. 4 5 Lines = 0 6 7 echo 8 9 cat myfile.txt | while read line; # (note: pipelines generate sub-shells) 10 do {11 echo $ line 12 (Lines ++); # increase the value of this variable to 13 # +, but the external loop cannot be accessed. 14 # subshell problems. 15} 16 done 17 18 echo "Number of lines read = $ Lines" #0 19 # Error! 20 21 echo "----------------------" 22 23 24 exec 3 <> myfile.txt 25 while read line <& 3 26 do {27 echo "$ line" 28 (Lines ++ )); # add the variable value 29 # + now the external loop can be accessed. 30 # no sub-shell, now it's okay. 31} 32 done 33 exec 3> &-34 35 echo "Number of lines read = $ Lines" #8 36 37 echo 38 39 exit 0 40 41 # The following rows are scripts result, the script will not come here. 42 43 $ cat myfile.txt 44 45 Line 1. 46 Line 2. 47 Line 3. 48 Line 4. 49 Line 5. 50 Line 6. 51 Line 7. 52 Line 8. |