plpr—lpr Preprocessor

Contributed by Tom Van Raalte

I thought you might want to use the following script around the office. It is a preprocessor for lpr that sends output to the “best” printer. [This shell script is written for a BSD or Linux system and you would use this command in place of lpr. It reads the output of the lpq command to determine if a specific printer is available. If not, it tries a list of printers to see which one is available or which is the least busy. Then it invokes lpr to send the job to that printer.]

#!/bin/sh # #set up temp file TMP=/tmp/printsum.$$ LASERWRITER=${LASERWRITER-ps6} #Check to see if the default printer is free? # # FREE=`lpq -P$LASERWRITER | awk ' { if ($0 == "no entries") { val=1 print val exit 0 } else { val=0 print val exit 0 } }'` #echo Free is $FREE # #If the default is free then $FREE is set, and we print and exit. # if [ $FREE -eq 1 ] then SELECT=$LASERWRITER #echo selected $SELECT lpr -P$SELECT $* exit 0 fi #echo Past the exit # #Now we go on to see if any of the printers in bank are free. # BANK=${BANK-$LASERWRITER} #echo bank is $BANK # #If BANK is the same as LASERWRITER, then we have no choice. #otherwise, we print on the one that is free, if any are free. # if [ "$BANK" = "$LASERWRITER" ] then SELECT=$LASERWRITER lpr -P$SELECT $* exit 0 fi #echo past the check bank=laserprinter # #Now we check for a free printer. #Note that $LASERWRITER is checked again in case it becomes free #during the check. # #echo now we check the other ...

Get sed & awk, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.