Skip to content

Instantly share code, notes, and snippets.

@walterjwhite
Created April 10, 2020 16:54
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save walterjwhite/4da9fdc077dbbbd8ac855eb66edae1e1 to your computer and use it in GitHub Desktop.
Save walterjwhite/4da9fdc077dbbbd8ac855eb66edae1e1 to your computer and use it in GitHub Desktop.
bash - cups - lpr - print duplex
#!/bin/bash
# @see: https://opensource.com/article/20/4/print-duplex-bash-script#!/bin/bash
# take any PDF file and print the odd pages first
# THEN, ask the user to put the paper back in the printer, face down
# THEN, print even pages
# take the default printer from the cups configuration
# OR manually set it
_PRINTER=$(grep DefaultPrinter -i /etc/cups/printers.conf | awk {'print$2'} | sed -e "s/>//")
if [ "$#" -lt "1" ]
then
echo -e "Document to print is required."
exit 1
fi
_DOCUMENT_COUNT=$#
_INDEX=1
_print() {
lpr -P $_PRINTER -o $@ "$_DOCUMENT"
}
_log() {
echo ${_DOCUMENT} - $1
}
_print_document() {
_log "Printing even pages"
_print page-set=even -o outputorder=reverse
_log "Place paper back into the printer in EXACT OUTPUT ORDER (face down in tray) then press ENTER"
read _IS_DONE
_log "Printing odd pages"
_print page-set=odd
}
_wait_between_documents() {
if [ "$_INDEX" -lt "$_DOCUMENT_COUNT" ]
then
_log "Press ENTER when document is finished printing ($_INDEX) $_DOCUMENT_COUNT"
read _IS_DONE
((_INDEX++))
fi
}
for _DOCUMENT in $@
do
_print_document
_wait_between_documents
done
@mdebusk
Copy link

mdebusk commented Jun 15, 2022

On my system (Ubuntu 20.04.04), grep DefaultPrinter -i /etc/cups/printers.conf issues a "Permission denied" error. I have to use sudo.

As I'm not connected to a printer right now, I can't test further.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment