Linux feature file descriptors, redirects, pipe breaks, tee commands

Source: Internet
Author: User
Tags stdin

This section discusses a few questions:

1. File descriptor.

2. Redirect.

3. Pipe break

4. The use of tee.

1. File descriptor.

all files in the Linux system. Both the folder and the device are files. How to distinguish different files? The difference here is not different from the suffix we see under Windows. Nor is it a list of the different files we see under Linux with the LS command. The file descriptor here is primarily about an identifier for the file operation. There is no identifier for no action. For example, two processes are simultaneously reading the same file.

How do you differentiate between these two read operations? This requires a file descriptor to distinguish it. Each time a file is opened, a file descriptor is created, and the descriptor that opens is generally larger than the file descriptor that is opened first.

For example: We can man the open function. Take a look at the return value:

[[email protected] ~]# man Open

This means that the file descriptor is an integer. Of course, the system defaults to a maximum of 1024. We might as well use ulimit-n to check it out.

We can also check: Man socket look at the effect:

This means that file descriptors can be generated in a variety of ways. Remember: The file descriptor is returned whenever the file is manipulated.

Then let's take a look at the 3 file descriptors the system comes with.

These three are the smallest file descriptors, and we manipulate the files ourselves, and the smallest of the file descriptors returned is starting from 4.

stdin Standard input The default device is the keyboard file descriptor is: 0

The command reads the input data that is required during execution from the standard input file. -Data from File

stdout standard output The default device is the display file descriptor: 1

After the command executes, the output is sent to the standard output file. --Result output to file

stderr standard Error The default device is the display file descriptor: 2

The command sends various error messages during execution to the standard error file, and the error message is sent to the file

Standard input, standard output, and standard error The default is to use the keyboard and monitor as the associated device to interact with the operating system to accomplish the most basic input, output operation.

We might as well use man stdin to look at: [[email protected] ~]# man stdin

The above is about file descriptors.

2. Redirect.

REDIRECT includes: Redirect output and redirect input:

REDIRECT output:

Saves the normal output of the command to the specified file, rather than directly on the display screen

REDIRECT output using the > ">>" action symbol

> overwrite files

> > append content

If the redirected output file does not exist, the file will be created

eg: View the current host's CPU type saved to the Kernel.txt file (instead of directly to the screen)

uname-p View CPU Type information

Append kernel version information and operating system information to Kernel.txt

Redirect the output to a file (overwrite and append two ways)

REDIRECT Input

The path to receive input from the command is changed from the default keyboard to another file. Instead of waiting for input from the keyboard

Reading data from a file

Operator: "<"

by redirecting the input, you can enables some interactive procedures to be done by reading files

Eg: when you use passwd to set a password. Always follow the prompts to enter the password more cumbersome

instead, the interactive process can be ignored by using redirect input instead. Automatically complete password settings ( combined with the-stdin option to identify standard inputs)

To use non-interactive to perform a password setting:

[email protected] ~]# Touch passwd.txt
[Email protected] ~]# vim Passwd.txt
[Email protected] ~]# Useradd Zhangsan
Useradd:user ' Zhangsan ' already exists
[Email protected] ~]# Useradd Lisi
[Email protected] ~]# passwd--stdin Lisi < Passwd.txt

Changing password for user Lisi.

Passwd:all authentication tokens updated successfully.

Redirect input is the data that is entered through the contents of the file

Error redirection:

Saves the error message (option or parameter error) that occurred during the execution of the command to the specified file instead of directly to the display

error message saved to file

operator: using 2>

2 Refers to the number of the error file (1 0 number omitted in the use of standard input and output)

in practical applications, error redirection can be used to collect error messages for execution. Provide a basis for troubleshooting;

for shell scripts, you can also redirect insignificant error messages to an empty file/dev/null to keep the script output simple

Eg: when backing up with the Tar command, a new error message is saved to the Err.log file

think of/dev/null as a "black hole". It is very equivalent to a write-only file. All content written to it will be lost forever. and trying to read from it is nothing. However,/dev/null is very useful for command lines and scripts .

echo $? Indicates whether the most recent operation was successful. 0 Success non-zero unsuccessful

The correct write to a file, the error in writing to a file

[Email protected] ~]# ls/tmp//nginx 1> a.txt 2>b.txt

Save to a picture more refreshing:

Output to the same file:

[email protected] ~]# ls/tmp//nginx/1>a.txt 2>&1

Or the following notation, redirect to 1 of the file can be omitted, because the default is 1.

[Email protected] ~]# ls/tmp//nginx/>a.txt 2>&1

&> Mixed Output

It's not right or wrong.

[email protected] ~]# ls/opt//mysql &> a.txt

3. Pipe break Here we only mention the pipe symbol, about the pipeline has the function that creates the pipeline specially, as well as the famous pipe and the nameless pipe, as well as the communication between pipeline, I put in the IPC communication elaboration. pipe character, you can connect two commands, the output of the preceding command as input of the following command.

Eg: see the nginx process.

[email protected] ~]# Ps-aux | grep nginx

Eg: View the port of a process:

4. Tree command.

Function: Reads the standard input data and outputs its contents to a file.
Syntax: Tee [-a][--help][--version][file ...]
The tee instruction reads data from a standard input device and outputs its contents to a standard output device while saving it as a file.
Parameters
-A or--append append
--help online Help.
--version displaying version information

Eg1

[[email protected] ~]# who View User Login information

root:0 2016-08-13 07:58 (: 0)
Root pts/0 2016-08-13 11:21 (: 0)

[[email protected] ~]# who | Tee who.out

Output The WHO results to the Who.out file, while the terminal displays:

Eg2

[[email protected] ~]# pwd| tee-a who.out Append

Note that the standard error output of the previous command is not read by the tee when using a pipe line.

[[email protected] ~]# ls-l YYY | Tee-a Who.out

The standard error is also read by Tee [[email protected] ~]# ls-l yyy 2>&1 | tee-a who.out

Linux feature file descriptors, redirects, pipe breaks, tee commands

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.