Linux shell standard input, standard output, error output

Source: Internet
Author: User

The shell can often be seen: 1>/dev/null 2>&1

Eg:sudo kill-9 ' ps-elf |grep-v grep|grep $1|awk ' {print $4} ' 1>/dev/null 2>/dev/null

The result of the command can be defined in the form of a%> output

/dev/null represents an empty device file
Where does the > delegate redirect to, for example: echo "123" >/home/123.txt
1 indicates stdout standard output, the system default value is 1, so ">/dev/null" is equivalent to "1>/dev/null"
2 indicates stderr standard error
& means equivalent to, 2>&1, 2 output redirect equals 1

Then the statement in the title of this article:
1>/dev/null First indicates that the standard output is redirected to an empty device file, that is, not outputting any information to the terminal, which is plainly not displaying any information.
2>&1 Then, the standard error output redirection is equivalent to the standard output because the standard error output is redirected to the empty device file because the standard output was previously redirected to an empty device file. /////////////////////

Scripts use only standard input, standard output, and standard errors

The shell will automatically open and close the three file descriptors for us 0, 1, 2, and we don't need to open or close them explicitly. The standard input is the input of the command, the default point to the keyboard, the standard output is the output of the command, the default point to the screen, the standard error is the output of the command error message, the default point to the screen.

If you do not explicitly redirect, the command reads the input from the screen through the file descriptor 0, outputting the output and error information to the screen through file descriptors 1 and 2. But if we want to read input or produce output from other files (again, the I/O device is also a file in unix/linux), we need to redirect the 0, 1, and 2 uses. Its syntax is as follows:


Command < filename redirects the standard input to the filename file
Command 0< filename redirects the standard input to the filename file

Command > FileName redirect the standard output to the filename file (overwrite)
Command 1> fielname redirect the standard output to the filename file (overwrite)

Command >> filename redirects the standard output to the filename file (append)
Command 1>> filename redirects the standard output to the filename file (append)

Command 2> filename redirects the standard error to the filename file (overwrite)
Command 2>> filename redirects the standard output to the filename file (append)

Command > FileName 2>&1 redirect standard output and standard error to filename file (overwrite)
Command >> filename 2>&1 redirect standard output and standard error to filename file (append)

Command < filename >filename2 REDIRECT standard input to the filename file and redirect the standard output

                                                           to filename2 file
command 0< filename 1> filename2   REDIRECT the standard input to the filename file, redirecting the standard output to

To the Filename2 file

The use of redirection has the following rules:

1) standard input 0, Output 1, error 2 need to be redirected separately, one redirect can only change one of them.
2) standard input 0 and standard output 1 can be omitted. (when it appears to the left of the redirect symbol)
3) The file descriptor is written directly to the left of the redirect symbol, and & is preceded by the right side.
4) There can be no space between the file descriptor and the redirect symbol!

REF:

Http://blog.sina.com.cn/s/blog_4aae007d010192qc.html

http://blog.itpub.net/35489/viewspace-702314/

1. Control of the standard input
Grammar:Commands < documentsMake the file the input to the command.
For example:
Mail-s "Mail Test" [email protected] < file1 the file file1 as the content of the letter, the main
The title is called Mail test and sent to the addressee.
2. Control of standard output
Grammar:Commands > DocumentsSends the execution result of the command to the specified file.
For example:
Ls-l > List writes the result of executing the "ls-l" command to the file list.
Grammar:command >! fileSends the execution result of the command to the specified file, overwriting if the file already exists.
For example:
LS-LG >! The list overwrites the results of the "LS-LG" command with the Write file list.
Grammar:Commands >& DocumentsWrites any information that is generated on the screen when the command executes to the specified file.
For example:
CC file1.c >& Error writes any information generated when compiling the file1.c file to the file error.
Grammar:Commands >> DocumentsAttaches the result of the command execution to the specified file.
For example:
Ls-lag >> List attaches the result of executing the "ls-lag" command to the file list.
Grammar:Commands >>& DocumentsAttaches to the specified file any information that is generated on the screen when the command executes.
For example:
CC FILE2.C >>& Error appends any information generated by the screen to the file error when compiling the file2.c file.

About input, output, and error outputs
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 you use these concepts in BASH, you will typicallyThe standard output is 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 > Ls_result
$ ls-l >> Ls_result

The above two commands redirect the result output of the LS command to the Ls_result file and append to the Ls_result 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 lost* 2> Err_result

This command has a "2" before the ">" Symbol,"2>" means redirecting standard error output。 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 lost* 2>>err_result command would produce?

What if the Find/home-name lost* > All_result are executed directly and the result is that only the standard output is stored in the All_result 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 lost*> All_result 2>& 1

The above example will firstREDIRECT standard error output to standard outputthen redirect the standard output to the All_result 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 lost*>&All_result

If the error message is not important, the following command allows you to avoid the interference of many useless error messages:

$ find/home-name lost* 2>/dev/null

Try some of the following redirection methods to see what the results are, and why?
$ find/home-name lost* > All_result 1>& 2
$ find/home-name lost* 2> All_result 1>& 2
$ find/home-name lost* 2>& 1 > All_result

Other than thata very useful redirect operator is "-", take a look at the following example:
$ (cd/source/directory && tar CF-.) | (Cd/dest/directory && tar xvfp-)
This command indicates that all files under the/source/directory directory are compressed and decompressed and moved quickly to the/dest/directory directory, and this command is/source/directory and/dest/directory A special advantage is shown when you 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

------------
Example:
$ make >& Compile.log
Redirect all information generated by the screen when compiling u-boot to the Compile.log file;

$ make > Compile.log 2>&1
The standard error output is redirected to the standard output first, and the standard output is redirected to the Compile.log file. This allows us to store all the output in a file.

$ make 2> Compile.log
The standard error output information is written to Compile.log, but the standard output is displayed on the screen. The compile-time error message is not displayed on the screen and will be written to Compile.log;

$ make 1> Compile.log
The standard output information is written to Compile.log, but the standard error output is displayed on the screen. The correct information at compile time is not displayed on the screen, and this information is written to Compile.log.

echo "pls enter the para" >&2
Give your mistake to a standard error

REF:

Http://book.51cto.com/art/200805/73657.htm

http://zqwt.012.blog.163.com/blog/static/1204468420103272857116/

http://my.oschina.net/emptytimespace/blog/112168

Http://www.doc88.com/p-6901992293656.html

Linux shell standard input, standard output, error output

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.