Working in the Macintosh environment, you’re used to a simple and elegant printer interface, particularly in Mac OS X, where Print Center makes it a breeze to add new printers and configure your existing printers. Until the advent of the Common Unix Printing System (CUPS), the Unix environment has never had a printing interface that even comes close in usability. As of Mac OS X 10.2, Print Center and CUPS are combined in a way that brings joy to command-line and GUI lovers alike.
When you add a printer with Print Center, you’ll only see some of the printer models that Mac OS X and CUPS support. To get access to advanced options, including extra print drivers, hold down the Option key when you click the Add button. To get even more printer drivers, download and install the Mac OS X release of gimp-print (http://gimp-print.sourceforge.net/). The gimp-print release is available as a disk image that includes a graphical installer.
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 might not look quite as nice. Further, some printers
accept only PostScript, which means you’ll need to
use a text-to-PostScript filter such as enscript
for good results. Before we cover printing itself,
let’s look at both pr
and
enscript
to see how they work.
The
pr
program 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.
PostScript is a page description language supported by some printer models. PostScript printers were once the norm among Macintosh users, and still are popular. If you’re using an inexpensive inkjet printer or a low- to mid-range laser printer, chances are good that your printer doesn’t support PostScript. Some of the utilities described in this section require PostScript, others don’t.
The syntax is:
pr option(s) filename(s)
pr
changes the format of the file only on the
screen or on the printed copy; it doesn’t modify the
original file. Table 5-1 lists some
pr
options.
Table 5-1. Some pr options
Option |
Description |
---|---|
|
Produces |
|
Double-spaces the output |
|
Prints |
|
Eliminates printing of header and top/bottom margins |
Other options allow you to specify the width of columns, set the page
length, etc. For a complete list of options, see the manpage,
man
pr
.
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
%
Let’s use pr
options to make a
two-column report with the header
“Restaurants”:
% pr -2 -h "Restaurants" food
Feb 4 9:58 2002 Restaurants Page 1
Sweet Tooth Isle of Java
Bangkok Wok Big Apple Deli
Mandalay Sushi and Sashimi
Afghani Cuisine Tio Pepe's Peppers
.
.
.
%
The text is output in two-column pages. The top of each page has the
date and time, header (or name of the file, if a header is not
supplied), and page number. To send this output to the default Mac OS
X printer instead of to the terminal screen, create a pipe to the
lpr
printer program:
% pr -2 -h "Restaurants" food | lpr
See Section 6.2 for more information on pipes.
One reason for the success of the Macintosh has been its integrated support of PostScript for printing. Allowing sophisticated imaging and high-quality text, PostScript printers are quite the norm in the Mac world. However, this proves a bit of a problem from the Unix perspective, because Unix commands are used to working with regular text without any special PostScript formatting included.
Translating plain text into PostScript is the job of
enscript
. The enscript
program
has a remarkable number of different command flags, allowing you
access to all the layout and configuration options
you’re familiar with from the Page Setup and Print
dialog boxes in Aqua.
The most helpful command flags are summarized in Table 5-2 (you can learn about all the many options to
enscript
by reading the
enscript
manpage). A typical usage is
enscript
-p -
Sample.txt
|
lpr
to send the file to a printer or
enscript
-psample.ps
sample.txt
to output to the file
sample.ps
.
Table 5-2. Useful enscript options
Option |
Description |
---|---|
|
Do not print page headers. |
|
Print body text using |
|
Print borders around columns (you can turn on multicolumn output with -1 or -2). |
|
Send output to |
|
Rotate printout 90 degrees, printing in landscape mode instead of portrait (the default). |
|
Output in the specified language. Default is PostScript, but
|
The other possibility for printing is
the standard Unix command lpr
for sending files to
your default printer (as chosen using Print Center). The syntax is:
lpr option(s) filename(s)
After you enter the command to print a file, the shell prompt returns to the screen and you can enter another command. However, seeing the prompt doesn’t mean your file has been printed. Your file has been added to the printer queue to be printed in turn.
To print a file named bills on the default
printer, use the lpr
command, as in this example:
% lpr bills
%
lpr
has no output if everything was accepted and
queued properly. If you need ID numbers for lpr
jobs, use the lpq
program to view the print queue
(see Section 5.1.4.1 later
in this chapter). The file bills will be sent to
the default system printer. lpr
has several
options. Table 5-3 lists three of them.
Table 5-3. Some lpr options
Command |
Description |
---|---|
|
Use given |
|
Print |
|
Notify sender by email when printing is done. |
Your system is probably not
configured properly for an lpr
printer. If you
have a named lpr
printer that works, try the
command again with the
-P
printername
option.
If not, you might want to try using atprint
or
opening up your files in TextEdit and printing from the Aqua
environment.
See whether the printer is printing now. If it is, other users may have made requests to the same printer ahead of you, and your file should be printed in turn. The following section explains how to check the print requests.
If no file is printing, check the printer’s paper supply, physical connections, and power switch. The printer may also be hung (stalled). If it is, ask other users or system staff people for advice.
The printer may not be configured to handle the kind of file
you’re printing. For instance, a file in plain-text
format will look fine when previewed in your Terminal window, but
look like gibberish when you try to print it. If the printer
understands only PostScript, make sure that you use
enscript
to translate the plain-text format into
acceptable PostScript.
If
you want to find out how many files
or “requests” for output are ahead
of yours in the printer queue, use the program named
lpq
. The lprm
command lets you
cancel print jobs from the lpr
queue.
Remember that you can also check on the status of print jobs by going into Applications → Utilities → Print Center. Double-click on the printer to see the state of the queue.
The lpq
command shows
what’s currently printing and
what’s in the queue for the default printer:
% lpq
Office_Jet is ready and printing
Rank Owner Job File(s) Total Size
active taylor 11 12.tiff 923648 bytes
1st taylor 12 Slashdot: News for nerds, stuff 231424 bytes
%
The first line displays the printer status. If the printer is
disabled or out of paper, you may see different messages on this
first line. Here you can see that the printer is ready for new print
jobs, and is currently printing. Jobs are printed in the order
indicated in the lpq
output. The
Job
number is important, because you can remove
print jobs from the queue (if you’re the owner) with
lprm
.
lprm
terminates lpr
requests. You can specify either the ID of the request (displayed by
lpq
) or the name of the printer.
If you don’t have the request ID, get it from
lpq
, then use lprm
. Specifying
the request ID cancels the request, even if it is currently printing:
% lprm 8
To cancel whatever request is currently printing, regardless of its
ID, simply enter lprm
and the printer name:
% lprm
lprm
does not provide any feedback unless it
encounters an error.
If you have an AppleTalk printer, there
is a set of easy-to-use AppleTalk-aware Unix commands included with
Mac OS X. The most important of the commands is
atprint
, which lets you easily stream any Unix
output to a printer.
To start working with the AppleTalk tools, run
atlookup
, which lists all the AppleTalk devices
recognized on the network (and that can be quite a few):
% atlookup
Found 4 entries in zone *
ff1d.a0.80 Dave Taylor's Computer:Darwin
ff01.04.08 PET:SNMP Agent
ff01.04.9d PET:LaserWriter
ff01.04.9e PET:LaserJet 2100
You can see that the PET printer (an HP LaserJet2100) appears with
two different AppleTalk addresses. Fortunately, that can safely be
ignored as well as the other AppleTalk devices that show up in the
list. The important thing is that the atlookup
command confirmed that there is indeed an AppleTalk printer online.
To select a specific AppleTalk printer as the default printer for the
atprint
command, run the oddly named
at_cho_prn
command. The trick is that you need to
run this command as root
or administrator. Use
the sudo
command (see “Superuser
Privileges with sudo” in Chapter 2) to run the program as
root
:
%sudo at_cho_prn
Password: 1: ff01.04.9dtPET:LaserWriter ITEM number (0 to make no selection)?1
Default printer is:PET:LaserWriter@* status: idle
(If you are on a multi-zone network, you’ll be
prompted to select a zone.) Now, finally, the PET printer is selected
as the default AppleTalk printer, and all subsequent invocations of
atprint
will be sent to that printer without
having to remember its exact name.
Because most of the printers available through AppleTalk on a
Macintosh network are PostScript printers, it’s
essential to use the enscript
program to ensure
the output is in proper PostScript format. As an example, the
following prints the intro manpage (an introduction to the manpage
system) on the PET printer, properly translated into PostScript:
% man intro | enscript -p - | atprint
man: Formatting manual page...
Looking for PET:LaserWriter@*.
Trying to connect to PET:LaserWriter@*.
[ 1 pages * 1 copy ] left in -
atprint: printing on PET:LaserWriter@*.
Pipes (command sequences with |
between the
commands) are covered in more detail in Chapter 6.
atprint
does not require a PostScript
printer (unless used with enscript
), but it does
require an AppleTalk printer.
18.224.38.43