翻译进度
4
分块数量
1
参与人数

Linux Shell 脚本:输入 / 输出重定向

这是一篇协同翻译的文章,你可以点击『我来翻译』按钮来参与翻译。


在本章中,我们将详细讨论 Shell 输入/输出重定向。大多数 Unix 系统命令从终端接收输入,并将结果输出发送回终端。命令通常从标准输入设备读取其输入,默认情况下,标准输入设备恰好是您的终端。类似地,命令通常将其输出写入标准输出设备,默认情况下,标准输出设备也是您的终端。

输出重定向

通常用于标准输出命令的输出可以很容易地转移到文件中。这种功能称为输出重定向。

如果将 > 文件 附加到通常将其输出写入标准输出设备的任何命令之后,则该命令的输出将被写入 文件 ,而不是您的终端。

检查下面的 who 命令,整个命令的输出结果被重定向到 users 文件中。

$ who > users

注意终端没有出现输出。 这是因为输出已从默认的标准输出设备(终端)重定向到指定的文件中。 您可以查看用户文件中的完整内容 −

$ cat users
oko         tty01   Sep 12 07:30
ai          tty15   Sep 12 13:32
ruth        tty21   Sep 12 10:10
pat         tty24   Sep 12 13:07
steve       tty25   Sep 12 13:03
$

如果命令将其输出重定向到文件,且该文件已经包含一些数据,则该数据将丢失。 考虑下面的例子−

$ echo line 1 > users
$ cat users
line 1
$

您可以使用 >> 操作符将输出添加到现有文件数据之后,如下所示 −

$ echo line 2 >> users
$ cat users
line 1
line 2
$
cn-five 翻译于 3年前

输入重定向

正如命令的输出可以重定向到文件,命令的输入也可以从文件中重定向。 由于 大于符号 > 用于输出重定向,因此 小于符号 < 用于输入重定向。

通常从标准输入获取的输入命令可以以这种方式将输入从文件中重定向。 例如,要计算上面示例中生成的文件 users 中的行数,可以执行如下命令 −

$ wc -l users
2 users
$

执行后,您将获得以下输出。 您也可以通过从文件 users 重定向到 wc 命令的标准输入来计数文件中的行数 −

$ wc -l < users
2
$

注意wc命令的两种形式产生的输出有差异。 在第一种情况下,会列出文件 users 的名称。但在第二种情况下则不会。

在第一种情况下,wc 命令知道它正在从文件 users 中读取其输入。 在第二种情况下,它只知道它正在从标准输入读取输入,所以它不显示文件名。

内嵌文档

内嵌文档 用于将输入重定向到交互式 shell 脚本或程序。

为交互式程序或交互式 shell 脚本提供所需的输入,我们就可以在 shell 脚本中运行交互式程序,而无需用户操作。

内嵌 文档的一般格式为 −

command << delimiter
document
delimiter

shell 将 << 运算符解释为读取输入的指令,直到找到包含指定分隔符的行。 所有直到包含分隔符的输入行被输入到命令的标准输入中。

分隔符告诉 shell 内嵌 文档已经完成。 没有它,shell 将永远继续取输入。分隔符必须是不包含空格或制表符的单个单词。

下面是对命令 wc-l 的输入,以计算总行数 -

$wc -l << EOF
   This is a simple lookup program 
    for good (and bad) restaurants
    in Cape Town.
EOF
3
$
cn-five 翻译于 3年前

You can use the here document to print multiple lines using your script as follows −

#!/bin/sh

cat << EOF
This is a simple lookup program 
for good (and bad) restaurants
in Cape Town.
EOF 

Upon execution, you will receive the following result −

This is a simple lookup program
for good (and bad) restaurants
in Cape Town.

The following script runs a session with the vi text editor and saves the input in the file test.txt.

#!/bin/sh

filename=test.txt
vi $filename <<EndOfCommands
i
This file was created automatically from
a shell script
^[
ZZ
EndOfCommands

If you run this script with vim acting as vi, then you will likely see output like the following −

$ sh test.sh
Vim: Warning: Input is not from a terminal
$

After running the script, you should see the following added to the file test.txt −

$ cat test.txt
This file was created automatically from
a shell script
$

Discard the output

Sometimes you will need to execute a command, but you don't want the output displayed on the screen. In such cases, you can discard the output by redirecting it to the file /dev/null −

$ command > /dev/null

Here command is the name of the command you want to execute. The file /dev/null is a special file that automatically discards all its input.

To discard both output of a command and its error output, use standard redirection to redirect STDERR to STDOUT −

$ command > /dev/null 2>&1

Here 2 represents STDERR and 1 represents STDOUT. You can display a message on to STDERR by redirecting STDOUT into STDERR as follows −

$ echo message 1>&2

Redirection Commands

Following is a complete list of commands which you can use for redirection −

Sr.No. Command & Description
pgm > file Output of pgm is redirected to file
pgm < file Program pgm reads its input from file
pgm >> file Output of pgm is appended to file
n > file Output from stream with descriptor n redirected to file
n >> file Output from stream with descriptor n appended to file
n >& m Merges output from stream n with stream m
n <& m Merges input from stream n with stream m
<< tag Standard input comes from here through next tag at the start of line
Takes output from one program, or process, and sends it to another

Note that the file descriptor 0 is normally standard input (STDIN), 1 is standard output (STDOUT), and 2 is standard error output (STDERR).

本文章首发在 LearnKu.com 网站上。

本文中的所有译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接
我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。

原文地址:

译文地址:https://learnku.com/server/wikis/36671

讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!