#!/bin/bash # Check if at least two arguments are provided (at least one source and one output) if [ "$#" -lt 2 ]; then echo "Usage: ${0##*/} output.pdf source1.pdf [source2.pdf ... sourceN.pdf]" exit 1 fi # The first argument is the output file output_file="$1" # The remaining arguments are the source PDF files shift # Remove the first argument from the list source_files="$@" # Run Ghostscript to merge the PDFs gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile="$output_file" $source_files # Check if the merge was successful if [ $? -eq 0 ]; then echo "Merged PDF successfully!" else echo "Error while merging PDFs!" exit 1 fi