Linux file descriptors and redirection

Source: Internet
Author: User
This article introduces the knowledge of file descriptors and redirection in linux. file descriptors are integers associated with file input and output. they are used to track opened files. For more information, see

In linux, the file descriptor is an integer associated with the file input and output. They are used to track opened files.
The most common file descriptors are stdin, stdout, and stderr. we can redirect the content of a file descriptor to another file descriptor.

The following are some examples of operations and redirection on file descriptors.

1.5.1 prerequisites

We frequently use standard input (stdin), standard output (stdout), and standard error (stderr) when writing scripts ).

Redirecting output to a file through content filtering is one of our basic tasks.

When a command outputs text, the output text may be an error message or a normal (non-error) output message.
By checking the output text itself, we cannot distinguish between normal output text and error text. However, we can solve this problem through file descriptors and extract the texts associated with specific descriptors.
A file descriptor is an integer associated with an opened file or data stream. File descriptors 0, 1, and 2 are reserved by the system.
0 -- stdin (standard input)
1 -- stdout (standard output)
2 -- stderr (standard error)

1.5.2 practical drills

Redirects or saves the output text to a file:


Copy codeThe code is as follows:
$ Echo "This is a sample text 1"> temp.txt

The content in the queue is cleared first.
Next, let's look at another example:
 

Copy codeThe code is as follows:
$ Echo "this is sample text 2"> temp.txt

This method will append the text to the target file.
> And> are different. Although both operators can redirect text to a file, the former clears the file and then writes the content, while the latter appends the content to the end of the existing file.
You can use the following method to view the file content:


Copy codeThe code is as follows:
$ Cat temp.txt
This is sample text 1
This is sample text 2

In linux, when the redirection operator is used, the redirected content is not displayed on the terminal, but is directly imported into the file. The redirection operator uses standard output by default. To use a specific file descriptor, you must place the descriptor before the operator.

> Equivalent to 1>; for>, the situation is similar (that is,> equivalent to 1> ).
To see what a standard error is and how to redirect it. When the command outputs an error message, the stderr information is printed. Consider the following example:
 

Copy codeThe code is as follows:
$ Is +
Is: cannot access +: No such file or directory

Here, "+" is an invalid parameter, because some will return an error message.
[Successful and unsuccessful commands
When a command is returned due to an error, it returns a non-zero exit status. when the command is successfully completed, it returns a number 0. The exit status can be changed from the special variable $? (Run echo $? immediately after the command is executed ?, You can print out the exit status).]

Print the stderr text to the screen instead of a file.


Copy codeThe code is as follows:
$ Is +> out.txt
Is: cannot access +: No such file or directory

In the following command, stdoutdoes not have any output, and the error message is redirected to out.txt.


Copy codeThe code is as follows:
$ Is + 2> out.txt # normal operation

You can redirect stderr to a single file and stdout to another file:


Copy codeThe code is as follows:
$ Cmd 2> stderr.txt 1> stdout.txt

You can also use the following method to convert stderr to stdout, so that both stderr and stdout are redirected to the same file:


Copy codeThe code is as follows:
$ Cmd 2> & 1 output.txt

Alternatively, use the following methods:


Copy codeThe code is as follows:
$ Cmd &> output.txt

Sometimes, the output may contain unnecessary information (such as the debugging information ). If you don't want the terminal to be filled with complicated topics about stderr, you can redirect stderr output to/dev/null to ensure that everything is cleared. Suppose we have three files: a1, a2, and a3. However, normal users do not have the "read, write, and execute" permission for a1. If you need to print the content of all files whose names start with a, you can use the cat command.
Set some test files:


Copy codeThe code is as follows:
$ Echo a1> a1
$ Cp a1 a2; cp a2 a3;
$ Chmod 000 a1 # clear all permissions

Although wildcard characters (a *) can be used to display all file content, the system displays an error message because file a1 has no read permission.


Copy codeThe code is as follows:
$ Cat *
Cat: a1 permission denied
A1
A1

Where cat: a1: permission denied belongs to stderr. We can redirect stderr information to a file, while stdout remains unchanged. Consider the following code:


Copy codeThe code is as follows:
$ Cat a * 2> err.txt mongostderris redirected to err.txt
A1
A1
$ Cat err.txt
Cat: a1: permission denied

Observe the following code:


Copy codeThe code is as follows:
$ Some_command 2>/dev/null

This section describes how to configure Linux file descriptors and redirect the second page.

In this example, the output from stderr is thrown to the file/dev/null. /Dev/null is a special device file, and any data received by this file will be discarded.

Therefore, a null device is usually a bit bucket or a black hole.
When stderr or stdout is redirected, the redirected text is passed into the file.

Because the text has been redirected to the file, there is nothing left to pass through the pipeline (|) to the next command, and these commands are received through stdin.
However, there is a clever way to redirect data to a file. on the other hand, you can also provide some copies of the redirected data as the stdin of subsequent commands.

Tee can be used for all this.

For example, to print stdout on a terminal and redirect it to a file, you can use tee as follows:


Copy codeThe code is as follows:
Command | tee FILE1 FILE2

In the following code, the tee command receives data from stdin. The slave writes a copy of stdoutto out.txt, and uses another copy as the stdin of subsequent commands. Run cat-n to add the row number before each row received from stdin and write it to stdout:


Copy codeThe code is as follows:
$ Cat a * | tee out.txt | cat-n
Cat: a1: permission denied
1 a1
2 a1

Out.txt content:


Copy codeThe code is as follows:
$ Cat out.txt
A1
A1

Note that cat: a1: permission denied does not appear in any file content. This is because the information belongs to stderr, and tee can only be read from stdin.
By default, the tee command overwrites the file, but provides the-a option for append content. For example:


Copy codeThe code is as follows:
$ Cat a * | tee-a out.txt | cat-n.

Commands with parameters can be written as follows: command FILE1 FILE2 and so on, or simply use command FILE.
We can use stdin as the command parameter. You only need to set "-" as the file name parameter of the command:


Copy codeThe code is as follows:
$ Cmd1 | cmd2 | cmd-

For example:


Copy codeThe code is as follows:
$ Echo who is this | tee-
Who is this
Who is this

Alternatively, we can use/dev/stdin as the output file name to use stdin.
Similarly, using/dev/stderr indicates a standard error, and/dev/stdout indicates a standard output. These special device files correspond to stdin, stderr, and stdout respectively.

Additional content:
Reading input commands from stdin can receive data in multiple ways. In addition, we can use cat and pipelines to develop our own file descriptors, for example:


Copy codeThe code is as follows:
$ Cat file | cmd
$ Cmd1 | cmd2

1. redirect files to commands
With redirection, we can read data from the file like using stdin:


Copy codeThe code is as follows:
$ Cmd

2. redirect text blocks inside the script
Sometimes text blocks (multi-line text) need to be redirected like standard input. Consider a special case: the source file is already in the shell script.

Let's take a look at the following shell script. write the header data to the log file, which can be completed as follows:


Copy codeThe code is as follows:
#! /Bin/bash
Cat < Log.txt
LOG FILE HEADER
This is a test log file
Function: System statistics
EOF

In cat < All text lines between log.txt and the next EOF line are treated as stdin data. The content of the log.txt file is printed as follows:


Copy codeThe code is as follows:
$ Cat log.txt
LOG FILE HEADER
This is a test log file
Function: System statistics

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.