From d716cdbe8c579587f1d27734c755dfa39ab66b71 Mon Sep 17 00:00:00 2001 From: Raman Gupta Date: Fri, 6 Aug 2021 12:49:03 -0400 Subject: [PATCH 1/2] Fix the multi-output loop Resolves #33. Fixes the arithmetic comparison in the while loop being incorrect. Also, handles the situation in which there are more output files being specified than scans available, in addition to the already handled reverse situation. --- scan | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scan b/scan index 836f92a..bea5fb0 100755 --- a/scan +++ b/scan @@ -283,10 +283,10 @@ if [[ $numscans > 0 ]]; then echo "Truncated $TRUNCPAGE pages" fi if [[ $numscans > 1 && $USEARRAY == 1 ]]; then - echo "Naming pdfs based on output list..." output_count=${#OUTPUT[@]} + echo "Naming $numscans pdfs based on output list of $output_count names..." index=0 - while [[ "$index" < "$output_count" ]]; do + while (( $index < $output_count && $numscans > $index )); do let "scanno = $index + 1" if [[ -f "${OUTPUT[$index]}" ]]; then mv "${OUTPUT[$index]}" "${OUTPUT[$index]}.orig" From 9ed52ab2faeed13e0eb95cc24bc98d219750c70d Mon Sep 17 00:00:00 2001 From: Raman Gupta Date: Fri, 6 Aug 2021 17:45:40 -0400 Subject: [PATCH 2/2] More arithmetic context improvements, corner cases * Handle truncate pages greater than or equal to the number of scans. --- scan | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/scan b/scan index bea5fb0..f805cf5 100755 --- a/scan +++ b/scan @@ -271,23 +271,34 @@ else MODE="--mode '$MODE'" fi eval scanadf -d \'"$DEVICE"\' $MAXPAGE $PGHEIGHT $PGWIDTH -S $SCRIPT --script-wait --resolution $RESOLUTION $MODE $DESKEW $CROP $DRIVER_OPTION $SOURCE -o $TMP_DIR/scan-%04d +# Simulate empty page scanner outputs for debugging +#convert xc:none -page Letter $TMP_DIR/scan-0001.pdf shopt -s extglob nullglob pdffiles=($TMP_DIR/scan-[0-9]*.pdf) numscans=${#pdffiles[@]} -if [[ $numscans > 0 ]]; then +if (( numscans > 0 )); then echo "Processing $numscans pages" - if [[ $numscans > $TRUNCPAGE && $TRUNCPAGE > 0 ]]; then - for x in ${pdffiles[@]:$numscans-$TRUNCPAGE:$TRUNCPAGE}; do rm "$x"; done; - pdffiles=(${pdffiles[@]:0:$numscans-$TRUNCPAGE}) - echo "Truncated $TRUNCPAGE pages" + if (( TRUNCPAGE > 0 )); then + truncpage=$TRUNCPAGE + if (( numscans < TRUNCPAGE )); then + truncpage=$numscans + fi + for x in ${pdffiles[@]:$numscans-$truncpage:$truncpage}; do rm "$x"; done; + pdffiles=(${pdffiles[@]:0:$numscans-$truncpage}) + echo "Truncated $truncpage pages" + let "numscans = numscans - truncpage" fi - if [[ $numscans > 1 && $USEARRAY == 1 ]]; then + if (( numscans <= 0 )); then + echo "Found no scans." + exit 0 + fi + if (( numscans > 1 && USEARRAY == 1 )); then output_count=${#OUTPUT[@]} echo "Naming $numscans pdfs based on output list of $output_count names..." index=0 - while (( $index < $output_count && $numscans > $index )); do - let "scanno = $index + 1" + while (( index < output_count && numscans > index )); do + let "scanno = index + 1" if [[ -f "${OUTPUT[$index]}" ]]; then mv "${OUTPUT[$index]}" "${OUTPUT[$index]}.orig" if [[ $APPEND == 1 ]]; then @@ -303,9 +314,9 @@ if [[ $numscans > 0 ]]; then else mv $TMP_DIR/scan-*(0)$scanno.pdf "${OUTPUT[$index]}" fi - let "index = $index + 1" + let "index = index + 1" done - elif [[ $numscans > 1 || $APPEND == 1 ]]; then + elif (( numscans > 1 || APPEND == 1 )); then echo "Concatenating pdfs..." if [[ -f "$OUTPUT" ]]; then mv "$OUTPUT" "${OUTPUT}.orig"