#!/bin/bash # caclurate the page counts of the two files pagesA=$(pdftk abclager1.pdf dump_data | awk '/NumberOfPages/ {print $2}') pagesB=$(pdftk abclager2_rev.pdf dump_data | awk '/NumberOfPages/ {print $2}') # determine which file has more pages if (( pagesA < pagesB )); then min=$pagesA max=$pagesB remainder="B" else min=$pagesB max=$pagesA remainder="A" fi # generate a list of pages up to the common last page for (( i = 1; i <= min; i++ )); do pages+=(A"$i" B"$i") done # append a list of remaining pages for (( i = min+1; i <= max; i++ )); do pages+=("$remainder$i") done pdftk A=abclager1.pdf B=abclager2_rev.pdf cat "${pages[@]}" output output.pdf