The following script scans PDFs via an auto-document feeder in Hp-Lip and then collates the pages to the correct order when there are reverse sides. No scans will be saved if no data is transmitted for the second set of scans. The second set of scans is the reverse of the pages.
#!/usr/bin/env bash
YMD=$(date +"%Y-%m-%d")
Y=$(date +"%Y")
WORKDIR=/home/username/Temp/
OUTPATH=/home/username/Scans/${Y}/
# Create a folder, ~/Temp and create a folder, ~/Scans
mkdir -p "/home/username/Scans/${Y}"
hp-scan --adf --mode=color &&
PART=hpscan
LATESTSCAN=`ls -t ${PART}*pdf | sed "1q"`
echo "${LATESTSCAN} ready for ${FILEOUTPUT}"
# Why you shouldn't parse the output of ls
# http://mywiki.wooledge.org/ParsingLs
# This does not use the FILEOUTPUT variable itself because the cp command
# seems to have trouble copying the file correctly, even with switches.
# Thanks Elektropepi at https://github.com/qpdf/qpdf/issues/259
function pdfscanmerge() {
if [ $# -ne 3 ]; then
echo "Usage: pdfscanmerge