In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces Linux how to achieve file descriptors and redirection, the article is very detailed, has a certain reference value, interested friends must read it!
The file descriptor under linux is the integer associated with the input and output of the file. They are used to track open files.
The most common file descriptors are stdin, stdout, and stderr. We can redirect the contents of one file descriptor to another.
Here are some examples of manipulating and redirecting file descriptors.
1.5.1 preliminary knowledge
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 the basic tasks we do.
When a command outputs text, the output text may be error messages or normal (non-error) output information.
Just by looking at the output text itself, we can't tell which is normal output text and which is error text. However, we can solve this problem by using file descriptors to extract the text associated with a particular descriptor.
A file descriptor is an integer associated with an open 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 actual combat exercise
Redirect or save the output text to a file:
The code is as follows:
$echo "This is a sample text 1" > temp.txt
This method stores the output text in the file temp.txt by intercepting the file, which means that the contents of the temp.txt are emptied before the output of the echo command is written to the file.
Next, let's look at another example:
The code is as follows:
$echo "this is sample text 2" > > temp.txt
This method appends text to the target file.
> and > > are not the same. Although both operators can redirect text to a file, the former empties the file before writing the content, while the latter appends the content to the end of the existing file.
You can view the contents of the file in the following ways:
The code is as follows:
$cat temp.txt
This is sample text 1
This is sample text 2
In the linux operating system, when the redirect operator is used, the redirected content does not appear in the terminal, but is directly imported into the file. The redirect operator defaults to standard output. If you want to use a specific file descriptor, you must put the descriptor before the operator.
> is equivalent to 1 >; for > >, the situation is similar (that is, > > equates to 1 >).
Let's 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:
The code is as follows:
$Is +
Is:cannot access +: No such file or directory
Here, + is an illegal parameter, so an error message will be returned.
[successful and unsuccessful commands
When a command has an error and returns, it returns a non-zero exit status, and when the command completes successfully, it returns the number 0. The exit status can be obtained from the special variable $? You can print out the exit status by running echo$?, immediately after the command executes the statement.]
Print stderr text to the screen instead of to a file.
The code is as follows:
$Is+ > out.txt
Is:cannot access+:No such file or directory
In the following command, however, stdout has no output because the error has been redirected to out.txt.
The code is as follows:
$Is + 2 > out.txt # is running normally
You can redirect stderr to a separate file and stdout to another file:
The code is as follows:
$cmd 2 > stderr.txt 1 > stdout.txt
You can also use the following methods to convert stderr to stdout so that both stderr and stdout are redirected to the same file:
The code is as follows:
$cmd 2 > & 1 output.txt
Or use the following methods:
The code is as follows:
$cmd& > output.txt
Sometimes, unnecessary information (such as debugging information) may be included in the output. If you don't want the terminal to be full of details about stderr, you can redirect the output of stderr to / dev/null to make sure everything is clean. Suppose we have three files, A1, a2, and a3. However, ordinary users do not have read-write-execute rights to A1. If you need to print the contents of all files whose filenames start with a, you can use the cat command.
Set up some test files:
The code is as follows:
$echo A1 > A1
$cp a1 a2 th cp a2 a3
$chmod 000A1 # clear all permissions
Although you can use the wildcard character (a*) to display all the contents of the file, an error message is displayed because there is no readable permission for the file A1.
The code is as follows:
$cat a *
Cat:a1 permission denied
A1
A1
Among them, cat:a1:permission denied belongs to stderr. We can redirect the stderr information to a file while the stdout remains the same. Consider the following code:
The code is as follows:
$cat a * 2 > err.txt # stderr is redirected to err.txt
A1
A1
$cat err.txt
Cat:a1:permission denied
Observe the following code:
The code is as follows:
$some_command 2 > / dev/null
This section is to play with Linux file descriptors and redirect the second page of the content, you are welcome to continue to read.
In this example, the output from stderr is thrown into the file / dev/null. / dev/null is a special device file and any data received by this file will be discarded.
As a result, null devices are also often referred to as bit bucket or black holes.
When a 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 pipe (|) to the following commands, which receive the text through stdin.
But there is an ingenious way to redirect data to a file on the one hand and provide some copies of the redirected data as stdin for subsequent commands on the other.
All of this can be done using tee.
For example: to print stdout in a terminal and redirect it to a file, you can use tee like this:
The code is as follows:
Command | tee FILE1 FILE2
In the following code, the tee command receives data from stdin. It writes a copy of the stdout to the file out.txt and uses another copy as the stdin for subsequent commands. The command cat-n prepends each line of data received from stdin with a line number and writes it to stdout:
The code is as follows:
$cat a * | tee out.txt | cat-n
Cat: a1: permission denied
1 a1
2 a1
View the contents of out.txt:
The code is as follows:
$cat out.txt
A1
A1
Note that cat:a1:permission denied does not appear in any of the file contents. This is because this information belongs to stderr, and tee can only be read from stdin.
By default, the tee command overwrites the file, but provides a-an option that can be used to append content. For example:
The code is as follows:
$cat a * | tee-an out.txt | cat-n.
Commands with parameters can be written as: command FILE1 FILE2 and so on, or simply use command FILE.
We can use stdin as the command parameter. You only need to use-as the file name parameter of the command:
The code is as follows:
$cmd1 | cmd2 | cmd-
For example:
The code is as follows:
$echo who is this | tee-
Who is this
Who is this
Or we can use stdin with / dev/stdin as the output file name.
Similarly, / dev/stderr is used for standard error and / dev/stdout for standard output. These special device files correspond to stdin, stderr and stdout, respectively.
Supplementary content:
Commands that read input from stdin can receive data in a variety of ways. In addition, we can use cat and pipes to develop our own file descriptors, such as:
The code is as follows:
$cat file | cmd
$cmd1 | cmd2
1. Redirect a file to a command
With redirection, we can read data from a file just as we did with stdin:
The code is as follows:
$cmd
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.