44 lines
1.2 KiB
Bash
Executable File
44 lines
1.2 KiB
Bash
Executable File
#/bin/bash
|
|
#
|
|
# Sends an ascii-art text file to a parallel port needle/dot-matrix printer.
|
|
#
|
|
# Usage: ./txt2printr.sh [ascii-art.txt]
|
|
# Env: PRINTER_DEV - printer device node (default: /dev/lp0)
|
|
|
|
infile="${1:-ascii-img.txt}"
|
|
# Source - https://stackoverflow.com/a/638980
|
|
# Posted by John Feminella, modified by community. See post 'Timeline' for change history
|
|
# Retrieved 2026-07-14, License - CC BY-SA 4.0
|
|
|
|
if [ -f /dev/lp0 ]; then
|
|
dev="${PRINTER_DEV:-/dev/lp0}"
|
|
fi
|
|
|
|
if [ -f /dev/usb/lp0 ]; then
|
|
dev="${PRINTER_DEV:-/dev/usb/lp0}"
|
|
fi
|
|
|
|
|
|
if [ ( ! -f /dev/lp0 ) && ( ! -f /dev/usb/lp0 )]; then
|
|
dev="${PRINTER_DEV:-/dev/stdout}"
|
|
fi
|
|
|
|
|
|
|
|
if [ ! -f "$infile" ]; then
|
|
echo "txt2printr: file not found: $infile" >&2
|
|
exit 1
|
|
fi
|
|
|
|
if [ ! -w "$dev" ]; then
|
|
echo "txt2printr: cannot write to printer device: $dev" >&2
|
|
echo "txt2printr: set PRINTER_DEV to override, or check permissions/parport module" >&2
|
|
exit 1
|
|
fi
|
|
|
|
{
|
|
printf '\x1b\x40' # ESC @ - reset printer (ESC/P and IBM Proprinter compatible)
|
|
sed 's/$/\r/' "$infile" # CRLF line endings, needed for raw writes to avoid staircasing
|
|
printf '\x0c' # form feed - eject the page
|
|
} > "$dev"
|