Using the Unix Shell to Duplicate Jobs to `lpr'

Some printers, print drivers or `lpr' commands are unable to duplicate print jobs with the "-#" (or "-n" for the `lp' command). In cases where the only a few copies are needed or access to a copying machine is nonexistent, the alternative is manually repeating the print commands. At the worst, this means repeatedly using the shell's command history and hitting the return key to manually print the desired number of copies.

Fortunately, the Unix shell can be programmed by Unix users to duplicate the jobs automatically. The following shell script wrapper for `lpr' (called `lprx') will imitate the behavior of duplicating copies. It simply runs the print command the specified number of times for you. Here's how to print 4 copies of a file named `'.

  lprx 4

Unfortunately, the `lprx' shell script cannot take files from the standard input (stdin). So, for instance these will not work:

  cat thought.h think.c | lprx 3 -Pprinter      # Will not work.
  pr README | lprx 100                          # Will not work.

The script could be corrected for this sitution. If a file is coming in on stdin, store it to a temporary file and provide the path to the temporary file on each execution of `lpr'. It currently does not do this. Fortunately, saving the result of a shell pipeline to a file is a trivial task.

  cat thought.h think.c > thought.txt
  lprx 3 -Pprinter thought.txt                  # Will work.

  pr README > README.print
  lprx 100 README.print                         # Will work.

The source code

Here is the source code to `lprx'.

Figure 1 -


Runs the \`lpr' command NUMBER times (when you're sure you want to).
Arguments after NUMBER are passed to \`lpr'";

n=`echo "$1" | sed -e 's/[^0-9]//'`

if [ "$n" != "$1" ] || [ $n -le 0 ]; then
  echo "invalid argument for lprx: $1"
  echo "$usage"
  exit 1


# Rather than using a for-loop, we opt for recursion.
if [ $n -gt 1 ]; then
  lpr "$@" # Print!
  n=`echo $n - 1 | bc`
  lprx $n "$@"
  lpr "$@"

Using it with `lp'

The script can be converted into the analogue for `lp' using the following `sed' command to transpose the contents of the script:

  sed -e s/LPR/LP/ -e s/lpr/lp/ >
  make lpx ## Requires GNU Make

Back to Unix Printing Tips.

Site maintained by Aaron Hawley
$Id: lprx.html,v 1.3 2007/09/07 15:52:35 aaronh Exp $

Permission is granted to copy, distribute and/or modify these web pages on "Unix Printing" under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, with the Front-Cover Text being "A free book about free software", and Back-Cover Texts being "You have freedom to copy and modify this book".