Unix 简明教程

Unix / Linux Basic Utilities - Printing, Email

在本章中,我们将详细讨论 Unix 基本实用工具中的打印和电子邮件。到目前为止,我们已尝试了解 Unix 操作系统及其基本命令的本质。在本章中,我们将学习一些可在日常生活中使用的重要 Unix 实用工具。

In this chapter, we will discuss in detail about Printing and Email as the basic utilities of Unix. So far, we have tried to understand the Unix OS and the nature of its basic commands. In this chapter, we will learn some important Unix utilities that can be used in our day-to-day life.

Printing Files

在 Unix 系统上打印文件之前,你可能希望重新整理格式以调整页边距、突出显示一些单词等。大多数文件也可以在不重新整理格式的情况下打印,但打印的原始文件可能并不可取。

Before you print a file on a Unix system, you may want to reformat it to adjust the margins, highlight some words, and so on. Most files can also be printed without reformatting, but the raw printout may not be that appealing.

许多版本的 Unix 包含两个功能强大的文本格式化程序, nrofftroff

Many versions of Unix include two powerful text formatters, nroff and troff.

The pr Command

pr 命令在终端屏幕或打印机上对文件进行微调格式。例如,如果你有一个文件中的长名字列表,你可以将其格式化到屏幕分别成两列或更多列。

The pr command does minor formatting of files on the terminal screen or for a printer. For example, if you have a long list of names in a file, you can format it onscreen into two or more columns.

以下是 pr 命令的语法 −

Following is the syntax for the pr command −

pr option(s) filename(s)

pr 仅在屏幕或打印副本上更改文件的格式;它不会修改原始文件。下表列出了一些 pr 选项 −

The pr changes the format of the file only on the screen or on the printed copy; it doesn’t modify the original file. Following table lists some pr options −

Sr.No.

Option & Description

1

-k Produces k columns of output

2

-d Double-spaces the output (not on all pr versions)

3

-h "header" Takes the next item as a report header

4

-t Eliminates the printing of header and the top/bottom margins

5

-l PAGE_LENGTH Sets the page length to PAGE_LENGTH (66) lines. The default number of lines of text is 56

6

-o MARGIN Offsets each line with MARGIN (zero) spaces

7

-w PAGE_WIDTH Sets the page width to PAGE_WIDTH (72) characters for multiple text-column output only

在使用 pr 之前,以下为一个名为 food 的示例文件的内容。

Before using pr, here are the contents of a sample file named food.

$cat food
Sweet Tooth
Bangkok Wok
Mandalay
Afghani Cuisine
Isle of Java
Big Apple Deli
Sushi and Sashimi
Tio Pepe's Peppers
........
$

让我们使用 pr 命令创建一个带 Restaurants − 头部的两列报告

Let’s use the pr command to make a two-column report with the header Restaurants −

$pr -2 -h "Restaurants" food
Nov  7  9:58 1997  Restaurants   Page 1

Sweet Tooth              Isle of Java
Bangkok Wok              Big Apple Deli
Mandalay                 Sushi and Sashimi
Afghani Cuisine          Tio Pepe's Peppers
........
$

The lp and lpr Commands

命令 lplpr 会将文件打印在纸上,而不是屏幕上显示。一旦使用 pr 命令对格式进行设置,便可以使用以下任何命令将文件打印到连接到计算机的打印机上。

The command lp or lpr prints a file onto paper as opposed to the screen display. Once you are ready with formatting using the pr command, you can use any of these commands to print your file on the printer connected to your computer.

你的系统管理员很可能已在你的站点设置了默认的打印机。若要在默认打印机上打印名为 food 的文件,请像以下示例那样使用 lplpr 命令 −

Your system administrator has probably set up a default printer at your site. To print a file named food on the default printer, use the lp or lpr command, as in the following example −

$lp food
request id is laserp-525  (1 file)
$

lp 命令会显示一个 ID,可用于取消打印作业或查看作业状态。

The lp command shows an ID that you can use to cancel the print job or check its status.

  1. If you are using the lp command, you can use the -n*Num* option to print Num number of copies. Along with the command lpr, you can use -Num for the same.

  2. If there are multiple printers connected with the shared network, then you can choose a printer using -d*printer* option along with lp command and for the same purpose you can use -P*printer* option along with lpr command. Here printer is the printer name.

The lpstat and lpq Commands

lpstat 命令显示打印机队列中的内容:请求 ID、所有者、文件大小、发送打印作业的时间,以及请求的状态。

The lpstat command shows what’s in the printer queue: request IDs, owners, file sizes, when the jobs were sent for printing, and the status of the requests.

如果你想查看除自己以外的所有输出请求,请使用 lpstat -o 。请求按打印顺序显示 −

Use lpstat -o if you want to see all output requests other than just your own. Requests are shown in the order they’ll be printed −

$lpstat -o
laserp-573  john  128865  Nov 7  11:27  on laserp
laserp-574  grace  82744  Nov 7  11:28
laserp-575  john   23347  Nov 7  11:35
$

lpq 提供的信息与 lpstat -o 略有不同 −

The lpq gives slightly different information than lpstat -o

$lpq
laserp is ready and printing
Rank   Owner      Job  Files                  Total Size
active john       573  report.ps              128865 bytes
1st    grace      574  ch03.ps ch04.ps        82744 bytes
2nd    john       575  standard input         23347 bytes
$

此处第一行显示打印机状态。如果打印机已禁用或缺纸,你可能在此第一行看到不同的消息。

Here the first line displays the printer status. If the printer is disabled or running out of paper, you may see different messages on this first line.

The cancel and lprm Commands

命令 cancel 可从 lp command 终结打印请求。 lprm 命令可终结所有 lpr requests 。你可以指定请求 ID(由 lp 或 lpq 显示)或打印机名称。

The cancel command terminates a printing request from the lp command. The lprm command terminates all lpr requests. You can specify either the ID of the request (displayed by lp or lpq) or the name of the printer.

$cancel laserp-575
request "laserp-575" cancelled
$

要取消当前正在打印的任何请求,无论其 ID 如何,只需输入 cancel 和打印机名称 −

To cancel whatever request is currently printing, regardless of its ID, simply enter cancel and the printer name −

$cancel laserp
request "laserp-573" cancelled
$

lprm 命令会在活动作业属于你的情况下将其取消。否则,你可以将作业号作为参数给出,或使用 dash (-) 删除你的所有作业 −

The lprm command will cancel the active job if it belongs to you. Otherwise, you can give job numbers as arguments, or use a dash (-) to remove all of your jobs −

$lprm 575
dfA575diamond dequeued
cfA575diamond dequeued
$

lprm 命令会告诉你实际从打印机队列中删除的文件名。

The lprm command tells you the actual filenames removed from the printer queue.

Sending Email

你使用终端邮件命令来发送和接收邮件。以下是发送电子邮件的语法 −

You use the Unix mail command to send and receive mail. Here is the syntax to send an email −

$mail [-s subject] [-c cc-addr] [-b bcc-addr] to-addr

以下是与邮件命令相关的部分重要选项 −s

Here are important options related to mail command −s

Sr.No.

Option & Description

1

-s Specifies subject on the command line.

2

-c Sends carbon copies to the list of users. List should be a commaseparated list of names.

3

-b Sends blind carbon copies to list. List should be a commaseparated list of names.

以下是一个将测试消息发送给 admin@yahoo.com 的示例。

Following is an example to send a test message to admin@yahoo.com.

$mail -s "Test Message" admin@yahoo.com

然后,您需要在行首键入您的消息,后跟 "control-D" 。要停止,只需键入点 (.) ,如下所示 −

You are then expected to type in your message, followed by "control-D" at the beginning of a line. To stop, simply type dot (.) as follows −

Hi,

This is a test
.
Cc:

您可以使用 redirect < operator 发送完整文件,如下所示 −

You can send a complete file using a redirect < operator as follows −

$mail -s "Report 05/06/07" admin@yahoo.com < demo.txt

要检查 Unix 系统中的传入电子邮件,只需键入电子邮件,如下所示 −

To check incoming email at your Unix system, you simply type email as follows −

$mail
no email