5.1 Formatting and Print Commands

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.

PostScript is a page-description language from Adobe supported by some printer models. PostScript printers were once the norm among Macintosh users and are still popular. If you're using an inexpensive USB 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. Refer to your printer's documentation (or the manufacturer's web site) to ascertain whether your printer supports PostScript.

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.

5.1.1 pr

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




Produces k columns of output


Double-spaces the output

-h header

Prints header at top of each page


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  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.

5.1.2 enscript

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




Do not print page headers.

-f font

Print body text using font (the default is Courier10).


Print borders around columns (you can turn on multicolumn output with -1 or -2).

-p file

Send output to file . Use - to stream output to standard out (for pipes).


Rotate printout 90 degrees, printing in landscape mode instead of portrait (the default).

-W lang

Output in the specified language. Default is PostScript, but enscript also supports HTML, overstrike, and RTF.

5.1.3 lpr

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 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



-P printer

Use given printer name if there is more than one printer at your site. The printer names are assigned in Printer Setup Utility.


Print # copies of the file.

-C name

Specify job name.


Print file should be formatted with a shaded information header containing filename, date, time, and page number. Useful only with text files.


Files printed should be deleted after completion of printing task (only for named files). Problem checklist

lpr returns "jobs queued, but cannot start daemon".

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, double check that your printer is set up and chosen as the default printer in Printer Setup Utility. You might want to try using atprint or opening up your files in TextEdit and printing from the Aqua environment.

My printout hasn't come out.

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 . Use the lpq command to ensure that it's still in the queue too.

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.

My printout is garbled or doesn't look anything like the file did on my terminal.

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.

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. lpq

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

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.

Learning Unix for Mac OS X Panther
Learning Unix for Mac OS X Panther
ISBN: 0596006179
EAN: 2147483647
Year: 2003
Pages: 88

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net