Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand Linux base shell redirection

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "how to understand Linux base shell redirection". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Standard input, standard output and standard error output

When linux shell executes the command, each process is associated with three open files and references them using file descriptors. Because the file descriptor is not easy to remember, shell also gives the corresponding file name:

File descriptor input file-standard input 0 (default is keyboard, 0 is output of file or other command) output file-standard output 1 (default is screen, 1 is file) error output file-standard error 2 (default is screen, 2 is file)

There are actually 12 file descriptors in the system, and we can use file descriptors 3 to 9 at will.

Standard input: enter data from the keyboard, that is, read the data from the keyboard.

Standard output: output data to the terminal.

Standard error output: outputs the standard error to the terminal.

The default standard input refers to the keyboard, and the default standard output and standard error output refers to the screen or terminal.

The system assigns the file identifier fd (file descripter) to the three files.

Under the Linux system, everything is a file, and the file identifier is generally used for the operation of the file. Their file identifiers are 0, 1, 1 and 2, respectively. Their relationship is as follows:

File descriptor name universal abbreviation default value 0 standard input stdin keyboard 1 standard output stdout screen 2 standard error stderr screen

two。 File redirection: change the input source and output location of the program

1. Output redirection:

Command > filename redirect standard output to a new file Command > > filename redirect standard output to a file (append) Command > filename redirect standard output to a file Command > filename 2 > & 1 redirect standard output and errors to a file Command 2 > filename redirect standard errors to a file Command 2 > > filename redirect standard output to a file (append ) Command > > filename2 > & 1 redirect standard output and errors to a file (append)

two。 Enter redirection:

Command

< filename >

The filename2Command command takes filename files as standard input and filename2 files as standard output Command

< filenameCommand命令以filename文件作为标准输入Command &m把标准输出重定向到文件描述符m中Command < &-关闭标准输入Command 0>

& ditto

III. Examples of use

The code is as follows:

Cmd > file

# description: send the output of cmd to the file file (override mode)

Cmd > > file

# description: send the output of cmd to file file (append mode)

Cmd

< file #说明: 以 file 文件的内容作为 cmd 的输入 cmd & file #说明: 将标准输出和标准错误都发送到文件 file (作用同上) cmd &>

File

# description: the function is the same as above, better format

Cmd & > > file

# description: send standard error and standard output to file file (append mode)

Cmd > f1 2 > f2

# description: send standard output to file F1 and standard error to file f2

Cmd | tee files

# description: send the output of cmd to standard output (usually terminal) and file files

Cmd 2 > & 1 | tee files

# description: send cmd output and errors to standard output (usually terminal) and file files

Cmd | & tee files

# description: same function as above

IV. Some advanced uses of shell redirection

1. Redirect standard error

Example 1:

The code is as follows:

Command 2 > / dev/null

If there is an error in command execution, redirect the error information to the empty device

Example 2:

The code is as follows:

Command > out.put 2 > & 1

Redirects the standard output and standard error performed by command to out.put (that is, the output is printed to out.put regardless of whether command executes correctly or wrongly).

2.exec usage

The exec command can be used to replace the current shell; in other words, there is no promoter shell, and when you use this command, any existing environment variables will be cleared and a shell will be restarted (re-enter the user name and password).

The code is as follows:

Exec command

Where command is usually a shell script.

Use it when operating on the file descriptor (and only at this time), it will not overwrite your current shell

Example 1:

The code is as follows:

#! / bin/bash

# file_desc

Exec 3

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report