Go Input/output redirection under Linux

Source: Internet
Author: User

Input and output redirection is supported in Linux environments, denoted by symbols < and >.
0, 1, and 2 represent standard input, standard output, and standard error message output, which can be used to specify the standard input or output that needs to be redirected, such as 2>lee.dat for outputting error information to file Lee.dat.
Also, redirects can be made between these three standard input outputs, such as redirecting error messages to standard output, which can be implemented with 2>&1.
Linux also has a special file/dev/null, it is like a bottomless pit, all the information redirected to it will disappear into a trace. This is useful when we do not need to echo all of the program's information, so we can redirect the output to/dev/null.
If you want both the normal output and the error message to be displayed, redirect both the standard output and the standard error to/dev/null, for example:
# ls 1>/dev/null 2>/dev/null
Another approach is to redirect errors to standard output and then redirect to/dev/null, for example:
# ls >/dev/null 2>&1
Note: The order here cannot be changed, otherwise the desired effect is not achieved, the standard output is redirected to/dev/null, then the standard error is redirected to standard output, and the standard error is redirected to/dev/null because the standard output has been redirected to/dev/null.
1. Control of the standard input
Syntax: Command < file as input to the command.
For example:
Mail-s "Mail Lee"[email protected]< lee1 file Lee1 as the content of the letter, the subject name is Mail Lee, sent to the recipient.
2. Control of standard output
Syntax: commands > Files
Sends the execution result of the command to the specified file.
For example:
Ls-l > Lee.dat writes the results of the "ls-l" command to the file Lee.dat.
Syntax: Command >! File
Sends the execution result of the command to the specified file, overwriting if the file already exists.
For example:
LS-LG >! Lee.dat overwrites the result of executing the "LS-LG" command into the file Lee.dat.
Syntax: Commands >& files
Writes any information that is generated on the screen when the command executes to the specified file.
For example:
CC lee.c >& Lee.dat writes any information generated when the lee.c file is compiled to the file Lee.dat.

Syntax: Commands >> files
Attaches the result of the command execution to the specified file.
For example:
Ls-lag >> Lee.dat Attaches the result of executing the "ls-lag" command to the file Lee.dat.

Syntax: Commands >>& files
Attaches to the specified file any information that is generated on the screen when the command executes.
For example:
CC LEE.C >& Lee.dat any information that is generated by the screen when compiling the lee.c file is appended to the file Lee.dat.

In a character terminal environment, the concept of standard input/standard output is well understood. Input refers to the input to an application or command, whether it is entered from the keyboard or from another file, and the output refers to some information generated by the application or command; Unlike Windows systems, there is a standard error output concept under Linux. This concept is mainly for the purpose of program debugging and system maintenance, error output in the standard output separate can let some advanced error information does not interfere with the normal output information, so as to facilitate the use of ordinary users.
In Linux systems: standard input (stdin) defaults to keyboard input, standard output (STDOUT) defaults to screen output, and standard error output (stderr) is output to the screen by default (Std above). When using these concepts in BASH, standard output is generally represented as 1, and the standard error output is represented as 2. Here are examples of how to use them, especially standard output and standard error output.
Input, output, and standard error outputs are primarily used for I/O redirection, which means that their default settings need to be changed. Let's look at this example:
$ ls > Lee.dat
$ ls-l >> Lee.dat
The above two commands redirect the result output of the LS command to the Lee.dat file and append to the Lee.dat file, instead of outputting to the screen. ">" is the symbol for the redirection of the output (standard output and standard error output), with two consecutive ">" symbols, or ">>" indicating that the original output is not cleared. Let's look at a slightly more complicated example:
$ find/home-name lee* 2> Err_lee
This command adds a "2" to the ">" Symbol, and "2>" indicates that the standard error output is redirected. Because some directories under the/home directory cannot be accessed due to permission restrictions, some standard error outputs are stored in the Err_result file. Can you imagine what the find/home-name lee* 2>>err_result command would produce?
What if the Find/home-name lee* > All_lee are executed directly and the result is that only the standard output is stored in the All_lee file, so that the standard error output and the standard input are stored in the file as well? Look at the following example:
$ find/home-name lee* > All_lee 2>& 1
The above example will first redirect the standard error output to the standard output, and then redirect the standard output to the All_lee file. This allows us to store all the output in a file. To achieve these functions, there is a simple way to do the following:
$ find/home-name lee* >& All_lee
If the error message is not important, the following command allows you to avoid the interference of many useless error messages:
$ find/home-name lee* 2>/dev/null
After the students go back can also test the following several redirection methods, see what results, why?
$ find/home-name lee* > All_lee 1>& 2
$ find/home-name lee* 2> All_lee 1>& 2
$ find/home-name lee* 2>& 1 > All_lee
Outside a very useful redirect operator is "-", see the following example:
$ (Cd/source/lee && tar CF-.) | (Cd/dest/lee && tar xvfp-)
This command indicates that all files under the/source/lee directory are compressed and decompressed and moved quickly to the/dest/lee directory, which shows a special advantage when/source/lee and/dest/lee are not in the same file system.
Here are a few more common uses:
n<&-means the n input is closed
<&-means to turn off standard input (keyboard)
n>&-means the n output is turned off
>&-indicates that standard output is turned off
Give us a very useful command:
grep Lee ' Find. 2>/dev/null|grep sh$ '

Go Input/output redirection under Linux

Related Article

Contact Us

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: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.