Unix 简明教程

Linux - Standard I/O Streams

Standard IO Streams 指的是由任何 Linux 或 Unix 系统默认提供的以下三个流。

Standard IO Streams refers to the following three streams which are provided by default by any Linux or Unix system.

  1. Stdin Stream

  2. Stdout Stream

  3. Stderr Stream

linux io standard streams

如前所解释,在 Linux 中,所有内容都是文件。与目录、常规文件一样,甚至设备都被视为特殊文件。每个文件都有一个关联的数字,称为文件描述符或简称为 FD,此 FD 用于对关联文件执行任何输入或输出操作。

As explained earlier, In Linux, everything is a file. Like directories, regular files, and even the devices are considered to be special files. Each file has an associated number which is called a File Descriptor or in short an FD and this FD is used to make any input or output operation on the associated file.

Stdin、Stdout 和 Stderr 是终端屏幕,它们的文件描述符分别为 0、1 和 2。

Stdin , Stdout and Stderr are terminal screen and their file descriptors are 0, 1, and 2 respectively.

Stdin Input Stream

这称为标准输入,关联的文件描述符为 0。这也表示为 STDIN 。Linux 程序将从 STDIN 读取默认输入。事实上,STDIN 指的是计算机屏幕。

This is referred to as the standard input and the associated file descriptor is 0. This is also represented as STDIN. The Linux program will read the default input from STDIN. Actually STDIN refers to the computer screen.

大多数 Linux 命令都支持标准 Stdin 流,这意味着我们无需使用文件或命令行输入,而可以直接使用 STDIN 向程序提供数据。

Most of Linux commands support standard Stdin stream which means instead of using files or command line input, we can feed data to the program directly using STDIN.

例如,cat 命令将从屏幕读取输入。键入以下命令并开始编写如下内容:

For example cat command will read input from the screen. Type the following command and start writing something as follows:

$ cat
This is text which I'm feeding using STDIN directly.
This is text which I'm feeding using STDIN directly.

$

此处,cat 命令获取标准输入,当您使用 Enter 键换到下一行,或使用 CTRL+D 退出,它会将其打印到屏幕(即您的标准输出)。

Here cat command takes standard input and when you go to next line by using Enter key or exit using CTRL+D, it prints the same input to the display which is your standard output.

当您编写 C、C++ 或 Java 程序并将其中断以从屏幕读取一些输入时,Stdin 流将用于从输入屏幕读取数据。您可以使用输入重定向运算符 < 将数据发送到 Stdin,如下所示:

When you write a C, C++ or Java program and halts it to read some input from the screen, then Stdin stream is used to read the data from the input screen. You can send data to Stdin using the input redirection operator < as follows

$ cat < inputfile.txt
This is text which I'm feeding using STDIN directly.


$

上述命令从 inputfile.txt 读取输入,并将其重定向到 Stdin,Stdin 最终会将数据提供给 cat 程序,以便在屏幕上显示。

Above command reads the input from inputfile.txt and redirect it to Stdin which is finally fed to the cat program to be displayed on the screen.

Stdout Output Stream

这称为标准输出,关联的文件描述符为 1。这也表示为 STDOUT。Linux 程序将在 STDOUT 写入默认输出。

This is referred to as the standard output and the associated file descriptor is 1. This is also represented as STDOUT. The Linux program will write the default output at STDOUT.

每当执行 Linux 程序时,其输出将发送到计算机屏幕的文件描述符,然后您会在显示屏上看到该输出。或者,您可以将程序输出发送到任何要保存的文件或任何要打印的打印机。

Whenever a Linux program is executed, it’s output is sent to your computer screen’s File Descriptor which is then you see on the display screen. Alternatively you can send your program output to any file to be saved or to any printer to be printed.

以下命令将输出发送到 Stdout,以便在您的屏幕上显示:

Following command sends the output to Stdout so that it can be displayed on your screen:

$ ls -l
total 8
-rw-r--r-- 2 root root 132 May  1 07:18 filename
-rw-r--r-- 2 root root 132 May  1 07:18 hardlink
lrwxrwxrwx 1 root root   8 May  1 07:17 symlink -> filename

$

您可以使用输出重定向运算符 > 将输出重定向到文件 output.txt,如下所示:

You can redirect your output to be saved in a file output.txt using the output redirection operator > as follows:

$ ls -l > output.txt


$

或者您可以使用以下命令:

Or you can use the following command:

$ ls -l 1 > output.txt


$

现在,没有输出将显示在屏幕上。此处,我们提到了 1 > output.txt,这意味着 Stdout 应重定向到 output.txt 文件。默认情况下,系统将其视为 Stdout。下一节将区分 Stdout 和 Stderr。

Now none of the output will be displayed on the screen. Here we mentioned 1 > output.txt which means Stdout should be redirected to output.txt file. By default system considers it as Stdout. Next section will differentiate Stdout from Stderr.

Stderr Error Stream

这称为标准错误,关联的文件描述符为 2。这也表示为 STDERR。Linux 程序将在 STDERR 写入所有错误消息。

This is referred to as the standard error and the associated file descriptor is 2. This is also represented as STDERR. The Linux program will write all the error messages at STDERR.

默认情况下,STDERR 是你的显示屏,但就像标准输出一样,它也可以被重定向到文件,用于保存错误信息。

By default STDERR is your display screen but just like standard out, it can be redirected to a file to save the error messages.

让我们尝试使用一个不存在的文件名来执行同样的 cat 命令:

Let’s try the same cat command with a file name which does not exit:

$ cat  nonexist.txt
cat: nonexist.txt: No such file or directory

$

nonexist.txt 这里文件不存在,因此默认情况下会将错误作为 stderr 显示在屏幕上。

Here nonexist.txt file does not exist, so an error is displayed on the screen as stderr by default.

您可以使用文件描述符 5 保存此错误消息,如下所示:

You can save this error message using the file descriptor 2 as follows:

$ cat  nonexist.txt 2>err

$

这次您的错误消息保存到 err 文件中。

This time your error message is saved in err file.