Bash 中使用重定向运算符的五种方法
数据通过stdin(通常是键盘)输入到计算机,结果输出到stdout(通常是 shell)。这些路径称为流。但是,可以改变这些输入和输出位置,导致计算机从stdin以外的某个地方获取信息或将结果发送到stdout以外的某个地方。此功能称为重定向。
在本文中,您将学习五个重定向操作符,包括一个用于stderr 的操作符。我提供了每个操作符的示例,并以一种您可以在自己的 Linux 系统上复制的方式呈现了这些内容。
常规输出 > 运算符
输出重定向器可能是最受认可的操作符。标准输出 ( stdout ) 通常指向终端窗口。例如,当您键入命令时date
,屏幕上将显示结果时间和日期输出。
[damon@localhost ~]$ date
Tue Dec 29 04:07:37 PM MST 2020
[damon@localhost ~]$
但是,可以将此输出从stdout重定向到其他地方。在本例中,我将把结果重定向到名为 的文件specifications.txt
。我将使用命令cat
查看文件内容来确认它是否有效。
[damon@localhost ~]$ date > specifications.txt
[damon@localhost ~]$ cat specifications.txt
Tue Dec 29 04:08:44 PM MST 2020
[damon@localhost ~]$
重定向器的问题>
在于它会覆盖文件中的任何现有数据。此时,文件中已经有了date
信息specifications.txt
,对吧?如果您键入hostname > specifications.txt
,输出将发送到文本文件,但它将覆盖先前步骤中现有的时间和日期信息。
[damon@localhost ~]$ hostname > specifications.txt
[damon@localhost ~]$ cat specifications.txt
localhost.localdomain
[damon@localhost ~]$
>
重定向器很容易因意外覆盖现有信息而引发麻烦。
[读者还喜欢: 你需要知道的 10 个基本 Linux 命令]
正则输出附加>>运算符
附加运算符将输出添加到现有内容中,而不是覆盖它。这允许您将多个命令的输出重定向到单个文件。例如,我可以使用运算符>>
重定向的输出,然后使用运算符将和重定向到文件。date
>
hostname
uname -r
specifications.txt
>>
[damon@localhost ~]$ date > specifications.txt
[damon@localhost ~]$ hostname >> specifications.txt
[damon@localhost ~]$ uname -r >> specifications.txt
[damon@localhost ~]$ cat specifications.txt
Tue Dec 29 04:11:51 PM MST 2020
localhost.localdomain
5.9.16-200.fc33.x86_64
[damon@localhost ~]$
注意:>>
重定向器甚至可以在空文件上工作。这意味着您可以想象
忽略常规>
重定向器以降低覆盖数据的可能性,并始终依赖>>
重定向器。这不是一个坏习惯。
常规输入 < 运算符
The input redirector pulls data in a stream from a given source. Usually, programs receive their input from the keyboard. However, data can be pulled in from another source, such as a file.
It's time to build an example by using the sort
command. First, create a text file named mylist.txt
that contains the following lines:
cat
dog
horse
cow
Observe that the animals are not listed in alphabetical order.
What if you need to list them in order? You can pull the contents of the file into the sort
command by using the <
operator.
[damon@localhost ~]$ sort < mylist.txt
cat
cow
dog
horse
[damon@localhost ~]$
You could even get a little fancier and redirect the sorted results to a new file:
[damon@localhost ~]$ sort < mylist.txt > alphabetical-file.txt
[damon@localhost ~]$ cat alphabetical-file.txt
cat
cow
dog
horse
[damon@localhost ~]$
Regular error 2> operator
The stdout displays expected results. If errors appear, they are managed differently. Errors are labeled as file descriptor 2 (standard output is file descriptor 1). When a program or script does not generate the expected results, it throws an error. The error is usually sent to the stdout, but it can be redirected elsewhere. The stderr operator is 2>
(for file descriptor 2).
Here is a simple example, using the misspelled ping
command:
[damon@localhost ~]$ png
bash: png: command not found...
[damon@localhost ~]$
Here is the same misspelled command with the error output redirected to /dev/null
:
[damon@localhost ~]$ png 2> /dev/null
[damon@localhost ~]$
The resulting error message is redirected to /dev/null
instead of the stdout, so no result or error message is displayed on the screen.
Note: /dev/null
, or the bit bucket, is used as a garbage can for the command line. Unwanted output can be redirected to this location to simply make it disappear. For example, perhaps you're writing a script, and you want to test some of its functionality, but you know it will throw errors that you don't care about at this stage of development. You can run the script and tell it to redirect errors to /dev/null
for convenience.
Pipe | operator
Ken Hess already has a solid article on using the pipe |
operator, so I'm only going to show a very quick demonstration here.
The pipe takes the output of the first command and makes it the input of the second command. You might want to see a list of all directories and files in the /etc
directory. You know that's going to be a long list and that most of the output will scroll off the top of the screen. The less
command will break the output into pages, and you can then scroll upward or downward through the pages to display the results. The syntax is to issue the ls
command to list the contents of /etc
, and then use pipe to send that list into less
so that it can be broken into pages.
[damon@localhost ~]$ ls /etc | less
Ken's article has many more great examples. Personally, I find myself using command | less
and command | grep string
the most often.
[ Download now: A sysadmin's guide to Bash scripting. ]
Wrap up
Redirect operators are very handy, and I hope this brief summary has provided you with some tricks for manipulating input and output. The key is to remember that the >
operator will overwrite existing data.