One point a day (1) file descriptor and redirection

Revisit the knowledge about file descriptors and redirection in Linux.

File descriptors and redirection

1.1 Related knowledge

1.1.1 Standard input and output, standard error

When executing a Shell command, three standard files are usually opened automatically, which are standard input (stdin), standard output (stdout), and standard error (stderr). stdin usually corresponds to the terminal keyboard, stdout and stderr correspond to the terminal screen. The relationship between the three is that the process gets the input data from the standard input file and outputs it to the standard output file normally, while the error information is sent to the standard error file.

1.1.2 File descriptor

How does the kernel distinguish these files? The answer is file descriptors . The file descriptor is a non-negative number. When opening an existing file or creating a new file, a file descriptor is returned. When reading and writing files, you also need to specify the file to be read through the file descriptor. Among them, file descriptors 0, 1 and 2 are reserved by the system.

 0 -- 标准输入(stdin)
 1-- 标准输出(stdoutT)
 2 -- 标准错误(stderr)

When writing scripts, we often come into contact with standard input and output and standard error. Correct handling of these contents can greatly reduce daily work and facilitate troubleshooting. We can manage the running log of the program by redirecting to the corresponding file.

1.2 Basic application cases

# echo "This is a simple sample text 1" > temp.txt

Can cat temp.txtbe found to view file contents, 'echo' of content output has been saved to a temp.txtfile and do not see any output terminal screen information.

1.2.1 Append output text to another file

# echo "This is a simple sample text 2" > temp.txt

This case has been added into the output document temp.txtfile
described: > and >>for the operator to redirect the file, >the first empty file, and then write the contents; >>content appended to the existing file.

cat temp.txt The contents of the files that can be viewed are:

# cat temp.txt
echo "This is a simple sample text 1
echo "This is a simple sample text 2

1.3 Basic use of file descriptors

In the Linux operating system, the redirection operator is used stdoutby default , so when the redirection operator is used, the redirected content will not appear in the terminal, but will be directly imported into the file. If I want to perform more complex processing, for example, I want to store the output of a certain script in the corresponding file; or if I have a higher requirement, how to specify the standard output of this script to a file and the standard error to another log file? ?

At this time, because there is a specified standard input and output involved, you need to combine file descriptors to achieve more stringent purposes when using redirection. The file descriptor needs to be placed before the operator, such as 1>. Easy to get after analysis, >equivalent to 1>; >>equivalent to 1 >>.

1.3.1 Standard error is printed to the screen

# ls +
ls: cannot access +: No such file or directory
# echo $?
2

+Illegal parameters, direct returns an error message when a command error occurs and exits, it returns an exit status of a non-zero; and when the command completes successfully, it returns the number 0 exit status from special variables $?obtained (in the command after executing the statement execution echo$?can print out the exit status)

1.3.2 Print standard error to file

# ls a > out.txt 
ls: cannot access a: No such file or directory
# ls a 2> out.txt 
# cat out.txt 
ls: cannot access a: No such file or directory

When performed ls +because of an error and exits behind the redirection operator failure. Plus file descriptor 2>specifies the output error to the specified file.

1.4 Comprehensive application of file descriptors and operators

1.4.1 Redirect to the same file

Both stderr and stdout are redirected to the same file output.txt. Here cmdindicates that the associated command is executed.

# cmd 2>&1 output.txt

&There is no fixed meaning, but it means that the target of redirection is not a file, but a file descriptor. For example, if it is 1>2, it looks like the file 1redirected to sound 2the same, so be &expressed operands.

1.4.2 Redirect to a different file

Redirect stderr to a file separately, and redirect stderr.txtstdout to another filestdout.txt

# cmd 2>stderr.txt 1>stdout.txt

1.5 Use of tee

1.5.1 Print stdout in the terminal and redirect to another file at the same time

To print stdout in the terminal and redirect it to a file at the same time, you can use tee like this:

# cmd| tee file1 file2

Another example is the following:

# cat out.txt |tee output.txt|cat -n
     1  This is a simple sample text1
     2  This is a simple sample text2
# cat output.txt 
This is a simple sample text1
This is a simple sample text2 

The tee command receives data from stdin. It writes a copy of stdout to the file out.txt, and also uses another copy as stdin for subsequent commands. The command cat -nadds the line number before each line of data received from stdin and writes it to stdout.

By default, tee command file will be overwritten, but offers an -aoption, can be used for additional content.

1.6 Use of /dev/null file

# cmd 2>/dev/null

Stderr output from this example will throw the file /dev/nullin. /dev/nullIt is a special device file. The data received by this file will be discarded. Equipment is also known as null 位桶or black holes.

Guess you like

Origin blog.csdn.net/qq_36148847/article/details/82070955