第二章 标准输出

Chapter 2 Standard Output

No software is worth anything if there is no output of some sort, but I/O has long been one of the nastier areas of computing. If you’re ancient, you remember the days when most of the work involved in running a program was setting up the program’s input and output. Some of the problems have gone away; for example, you no longer need to get operators to mount tapes on a tape drive (at least, not on any laptop or desktop system that we’ve seen!). But many of the difficulties are still with us.

如果没有某种类型的输出,那么任何软件都没有价值,但 I/O 长期以来一直是计算领域中最恶劣的领域之一。如果你的经历够长,你一定记得那种运行一个程序的大部分工作内容都是设置程序的输入和输出的日子。现在,有些问题已经不复存在;例如,你不再需要让操作员把磁带装入磁带驱动器(至少,我们现在见过的笔记本和桌面系统不再需要了)。但是仍有许多困难伴随着我们。

One problem is that there are many different types of output. Writing something on the screen is different from writing something in a file——at least, it sure seems different. Writing something in a file also seems different from writing it on a tape, or in flash memory, or on some other kind of device. And what if you want the output from one program to go directly into another program? Should users have to know how to connect the programs they want to run to different kinds of devices? That’s not a very good idea, either.

其中一个问题是,存在许多不同的输出类型。 在屏幕上输出内容不同于在文件中写内容——至少,两种类型确实看起来不同。 在文件中写入内容似乎与在磁带上、闪存中或其他类型的设备上写入内容有所不同。 如果你想让一个程序的输出直接输入到另一个程序怎么办? 用户是否必须知道如何将他们想要运行的程序连接到不同类型的设备? 这同样不是一个好的想法。

One of the most important ideas behind the Unix operating system was that everything looked like a file (an ordered sequence of bytes). The operating system was responsible for this magic. It didn’t matter whether you were writing to a file on the disk, the terminal, a tape drive, a memory stick, or something else; your program only needed to know how to write to a file, and the operating system would take it from there. That approach greatly simplified the problem.

Unix 操作系统中最重要的一个思路是,所有东西看起来都像一个文件(一个有序的字节序列)。操作系统负责实现这种魔法。无论您是要写入磁盘上的文件、终端、磁带驱动器、记忆棒还是其他内容,看上去都没有差别;您的程序只需要知道如何写入文件,操作系统就知道从哪儿能访问。 这种方法大大简化了此问题。

The next question was simply, “Which file?” How does a program know whether to write to the file that represents a terminal window, a file on the disk, or some other kind of file? Simple: that’s something that can be left to the shell.

下一个问题很简单,“哪个文件?”程序如何知道写入的文件是否代表的是终端窗口、磁盘上的文件或其他类型的文件? 很简单:这些事儿可以留给 shell。

When you run a program, you still have to connect it to output files and input files (which we’ll explore in the next chapter). That task doesn’t go away, but the shell makes it trivially easy. A command as simple as:

reads its input from inputfile and sends its output to outputfile. If you omit the > outputfile, the output goes to your terminal window. If you omit the < inputfile, the program takes its input from the keyboard. The program  literally doesn’t know where its output is going, or where its input is coming from. You can send the output anywhere you want (including to another program) by using bash’s redirection facilities.

运行程序时,仍然需要将程序连接到输出文件和输入文件(我们将在下一章中讨论)。这个任务不可避免,但是 shell 让这个问题变得非常简单。 一个简单的命令如下:

从 inputfile 中读取内容,并且把程序的输出送到 outputfile 中。如果你省略 > outputfile,则输出内容会跑到中断窗口。如果你省略 < inputfile,则程序会从键盘获取输入。该程序字面上不知道其输出的去向,也不知道其输入来自何处。你可以把通过使用 bash 的重定向功能将输出送到你想要的任何地方(包括其他程序)。

But that’s just the start. In this chapter, we’ll look at ways to generate output, and the shell’s methods for sending that output to different places.

但那只是一个开始。 在本章中,我们将介绍生成输出的方法,以及将该输出发送到不同位置的 shell 方法。

发表评论

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据