Make the following prompt check if the second argument is provided and if not, append "-compressed" to the pdf name and use that.
gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dQUIET -dPDFSETTINGS=/default -sOutputFile=21
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
To modify the provided Ghostscript command so that it checks if the second argument is provided, and appends -compressed to the output filename if it is not, you can use a simple shell script that includes a conditional check.
Here is a clean and robust version of the script:
bash
#!/bin/bash
# Check if the second argument is provided
if [ -z "$2" ]; then
# If not, construct the output filename by appending "-compressed" to the input filename
output_file="${1%.pdf}-compressed.pdf"
else
# Otherwise, use the provided second argument as the output file
output_file="$2"
fi
# Run Ghostscript with the constructed output file
gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dQUIET -dPDFSETTINGS=/default -sOutputFile="$output_file" "$1"
✅ Explanation:
[ -z "$2" ]: Checks if the second argument ($2) is empty.
${1%.pdf}: Removes the .pdf extension from the input filename ($1).
-compressed.pdf: Appends -compressed before the .pdf extension.
Quoting ("$1" and "$output_file"): Ensures that filenames with spaces or special characters are handled correctly.
📌 Example Usage:
Case 1: Provide both input and output files
bash
./script.sh input.pdf output.pdf
→ Uses output.pdf as the output.
Case 2: Only provide the input file
bash
./script.sh input.pdf
→ Creates input-compressed.pdf as the output.
This approach ensures the script behaves predictably and safely in most common scenarios, especially when working with PDF files.