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 does shell output redirect

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the knowledge of "how to redirect shell output". 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!

Linux Shell redirection is divided into two types, one is input redirection, the other is output redirection; literally, input and output redirection means "change the direction of input and output".

Like output redirection, the Unix command can also get input from a file with the syntax:

Command1

In this way, the commands that would otherwise need to get input from the keyboard are transferred to the file to read.

Note: output redirection is greater than sign (>), input redirection is less than sign (

Instance followed by the above example, we need to count the number of lines in the users file and execute the following command:

$wc-l users 2 users

You can also redirect input to a users file:

$wc-l

Note: the results of the above two examples are different: the first example outputs the file name; the second does not, because it only knows how to read from standard input.

Command1 outfile

Replace both input and output, execute command1, read from the file infile, and then write the output to outfile.

Redirect in-depth explanation

In general, three files are opened when each Unix/Linux command runs:

Standard input file (stdin): the file descriptor of stdin is 0Magna Unix program that reads data from stdin by default. Standard output file (stdout): the file descriptor of stdout is 1Magna Unix program to output data to stdout by default. Standard error file (stderr): the file descriptor of stderr is 2. The Unix program writes an error message to the stderr stream. By default, command > file redirects stdout to file,command

If you want stderr to be redirected to file, you can write:

$command 2 > file

If you want stderr to be appended to the end of the file file, you can write:

$command 2 > > file

2 represents a standard error file (stderr).

If you want to merge stdout and stderr and redirect to file, you can write:

$command > file 2 > & 1 or $command > > file 2 > & 1

If you want to redirect both stdin and stdout, you can write:

$command file2

The command command redirects stdin to file1 and stdout to file2.

This is the end of "how to redirect shell output". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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