Sorry, I forgot why I saved this script. I’ll come back to it later and edit. Or maybe I won’t.


The error “unexpected EOF while looking for matching "' typically indicates that there’s an incomplete or mismatched quoting or escaping in the script. This can happen if there’s a missing closing quote or an unbalanced set of quotes.

Let’s review the script again and ensure that all quotes and functions are properly closed and matched. Here’s the corrected script:

#!/bin/bash

# Function to remove leading and trailing single quotes
strip_quotes() {
    local path="$1"
    path="${path#"'}   # Remove leading single quote
    path="${path%"'}   # Remove trailing single quote
    echo "$path"
}

# Prompt for source directory
read -ep "Enter the path to the source directory containing PNG screenshots: " SOURCE_DIR

# Strip any leading or trailing single quotes from SOURCE_DIR
SOURCE_DIR=$(strip_quotes "$SOURCE_DIR")

# Validate source directory
if [ ! -d "$SOURCE_DIR" ]; then
    echo "Error: Source directory '$SOURCE_DIR' does not exist or is not a directory."
    exit 1
fi

# Prompt for destination directory
read -ep "Enter the path to the destination directory for compressed screenshots: " DEST_DIR

# Strip any leading or trailing single quotes from DEST_DIR
DEST_DIR=$(strip_quotes "$DEST_DIR")

# Create the destination directory if it doesn't exist
mkdir -p "$DEST_DIR"

# Loop through all PNG files in the source directory
for file in "$SOURCE_DIR"/*.png; do
    if [[ -f "$file" ]]; then
        filename=$(basename "$file")
        echo "Compressing $filename..."

        # Compress using pngquant and save to the destination directory
        pngquant --force --ext .png - < "$file" > "$DEST_DIR/$filename"
    fi
done

echo "Compression complete. Compressed screenshots are saved in: $DEST_DIR"

Changes Made:

  1. Function strip_quotes():

    • The function strip_quotes() is used to remove leading and trailing single quotes from a given path. It ensures that any path input, whether with or without single quotes, is processed correctly.
  2. Prompt (read -ep):

    • Used read -ep to prompt for input paths (SOURCE_DIR and DEST_DIR). The -e option enables readline support, allowing for line editing, and -p displays a prompt. This ensures a more interactive experience when entering directory paths.
  3. Path Processing:

    • Before using the entered directory paths (SOURCE_DIR and DEST_DIR), they are passed through strip_quotes() to ensure any extraneous single quotes are removed.
  4. Error Handling:

    • Checks if the source directory exists and is a valid directory before proceeding with compression. This helps avoid errors due to non-existent directories or incorrect paths.

Ensure you copy and paste this updated script exactly as shown into your shell script file. This version should resolve the “unexpected EOF” error and properly handle directory paths with or without single quotes, allowing you to drag and drop paths from your file manager without issues.