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 print out 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.
If you don't have a PostScript printer and are working in Unix, don't despair: almost all of Unix is text-oriented, so even a basic inkjet printer will be able to print code listings, simple email messages, and manpages without a hiccup.
The pr program does minor formatting of files on the Terminal 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 .
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
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 Sep 24 12:41 2003 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 in Chapter 6 for more information on pipes. The lpr program will be discussed in more detail later in this chapter.
pr does not require a PostScript printer.
One reason for the success of the Macintosh is its integrated support of PostScript for printing. Allowing sophisticated imaging and high-quality text, PostScript printers are 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 to send the file to a printer:
$ enscript -p - Sample.txt lpr [ 1 pages * 1 copy ] left in - $
Enscript can also produce PostScript output files for distribution in electronic form: enscript -psample.eps sample.txt translates sample.txt into PostScript and saves the resultant output to the file sample.eps .
Table 5-2. Useful enscript options
The underlying printing command in Unix is the command lpr , which sends files or the input stream to your default printer (as chosen using the Printer Setup Utility). 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 188.8.131.52 later in this chapter). The file bills will be sent to the default system printer. lpr has a number of options, most of which aren't useful in the Mac OS X Unix environment. Table 5-3 lists the most useful of them.
Table 5-3. The most useful lpr options
184.108.40.206 Problem checklist
lpr does not require a PostScript printer.
5.1.4 Viewing the Printer Queue
If you want to find out how many files or "requests" for output are ahead of yours in the printer queue, use the program 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 Printer Setup Utility. 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 LaserJet is ready and printing Rank Owner Job File(s) Total Size 1st taylor 5 (stdin) 1024 bytes 2nd taylor 6 Microsoft Word - ch05.doc 190464 bytes 3rd taylor 8 TINTIN.COM 30720 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 LaserJet
lprm does not provide any feedback unless it encounters an error.
5.1.5 Working with AppleTalk Printers
If you have an AppleTalk-based printer, or want to use a network printer that's accessible on your AppleTalk network, 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 * ff41.d0.80 Dave Taylor's Computer:Darwin ff01.04.08 LJ2100TN-via-AppleTalk:SNMP Agent ff01.04.9d LJ2100TN-via-AppleTalk:LaserWriter ff01.04.9e LJ2100TN-via-AppleTalk:LaserJet 2100
You can see that the LJ2100TN 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 Section 3.3 in Chapter 3) to run the program as root :
$ sudo at_cho_prn Password: Zone:*??????@??`??Pp???????@??`??RH??????????RP? 1: ff01.04.9dtLJ2100TN-via-AppleTalk:LaserWriter ITEM number (0 to make no selection)? 1 Default printer is:LJ2100TN-via-AppleTalk:LaserWriter@* status: idle
If you are on a multizone network, you'll be prompted to select a zone first.
Now, finally, the LaserJet 2100 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 LaserWriter printer, properly translated into PostScript:
$ man intro enscript -p - atprint Looking for LJ2100TN-via-AppleTalk:LaserWriter@*. Trying to connect to LJ2100TN-via-AppleTalk:LaserWriter@*. [ 1 pages * 1 copy ] left in - atprint: printing on LJ2100TN-via-AppleTalk:LaserWriter@*. $
Pipes (command sequences with a pipe ( ) 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.