2008-08-05

lplist - show solaris "lp-lite" queues matching pattern


This is something I've been using for years in various variations. "lp lite" was introduced as an alternative for the complexity of the sysv lpsched printer daemon. Instead of directories with printer filters and interface scripts, the assumption was that you'd be talking to a remote BSD print spooler.
For that, you needed the printer address and printer name, nothing more. Paper is typically A4 or Letter, depending on geography. What we print is typically postscript, or plain text. All of those are assumed to be preset on the other end.

This little script will scan your printers.conf (using lpget list) and dump all printer definitions matching your supplied regular expression pattern one line at a time, giving the queue name first, followed by all 'lpset -a' keyword=value settings for that queue.

By putting the queue name first in the output, this makes it easy input for the next script in a pipe, as you can simply say
lplist hp | while read hpq more; do ...; done
to process all print queues with "hp" in their name.

The empty pattern will just dump all queue names.

#!/bin/ksh -p
#
# usage: lplist [pattern]
#
#
PATH=/usr/bin

lpget list |
nawk -F: -v pattern=$1 '\
/:/ {
if (!pattern || q~pattern) {
print q, def
}
q=$1
def=""
next
}
!pattern || q ~ pattern {
def=def" -a "$0
}
END { if (def) print q,def }'



Exercise for the reader: Rewrite in pure awk. That is, drop the shell.