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

Shell--10, Shell input / output redirection

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

Most UNIX system commands accept input from your terminal and send the resulting output back to your terminal. A command usually reads input from a place called standard input, which happens to be your terminal by default. Similarly, a command usually writes its output to standard output, which is also your terminal by default.

The list of redirect commands is as follows:

The command instructs command > file to redirect the output to file. Command

< file将输入重定向到 file。command >

> file redirects the output to file by appending. N > file redirects the file with the file descriptor n to file. N > > file redirects the file with the file descriptor n to file as an append. N > & m merges the output files m and n. N > operator.

Example

Execute the following who command, which redirects the complete output of the command in the user file (users):

$who > users

After execution, no information is output at the terminal because the output has been redirected from the default standard output device (terminal) to the specified file.

You can use the cat command to view the contents of the file:

$cat users_mbsetupuser console Oct 31 17:35 tianqixin console Oct 31 17:35 tianqixin ttys000 Dec 1 11:33

Output redirection overwrites the contents of the file, as shown in the following example:

$echo "rookie" > users$ cat users rookie $

If you do not want the contents of the file to be overwritten, you can use > > to append to the end of the file, for example:

$echo "rookie" > > users$ cat users rookie $input redirection

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

Command1

< file1 这样,本来需要从键盘获取输入的命令会转移到文件读取内容。 注意:输出重定向是大于号(>

), the input redirection is the less than sign (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

< file 将stdin 重定向到 file。 如果希望 stderr 重定向到 file,可以这样写: $ 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

< file1 >

File2

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

Here Document

Here Document is a special redirect method in Shell that redirects input to an interactive Shell script or program.

Its basic form is as follows:

Command & 1

Note: 0 is standard input (STDIN), 1 is standard output (STDOUT), and 2 is standard error output (STDERR).

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

Servers

Wechat

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

12
Report