3

I'm having trouble with GhostScript accepting shell script variables for filenames, if those filenames have spaces in them.

gs -dPDFX -dNOPAUSE -dBATCH -dNOOUTERSAVE -sDEVICE=pdfwrite -sOutputFile="${filename}" -sColorConversionStrategy=CMYK -dProcessColorModel=/DeviceCMYK -dCompatibilityLevel=1.4  "${f}" /usr/local/share/ghostscript/9.20/lib/PDFX_def.ps

If the filename has a space, then GS says "no such file", giving the name as:

path/to/file/partial\

(Note the final backslash.)

I'm also getting: Error: /undefinedfilename in (/Users/Ben/Desktop/qwe\\ qwe.pdf)

where it's taking the escape literally. I can't believe such a venerable piece of software has such a major failing, though others seem to have had the same problem, without any significant solution. Is there anything I can do to my script?

I'm on MacOS 10.11 and 12. Oh and "don't have spaces" is not a solution. ;-)

UPDATE: Even the very simplest example shows the error:

> f="/Users/Ben/Desktop/qwe\ qwe.pdf"
> gs "$f"
GPL Ghostscript 9.20 (2016-09-26)
Copyright (C) 2016 Artifex Software, Inc.  All rights reserved.
This software comes with NO WARRANTY: see the file PUBLIC for details.
Error: /undefinedfilename in (/Users/Ben/Desktop/qwe\\ qwe.pdf)
4
  • You need to put "" round the filename, I suspect suspect your shell script is stripping them. Try it from the command line.
    – KenS
    Mar 3 '17 at 17:23
  • Is that not what I have already done?
    – benwiggy
    Mar 3 '17 at 18:04
  • It seems that GhostScript doesn't like the escape backslash. If I remove the backslash, leaving the space "unescaped", then it works. How do I tell bash not to put escape characters in my file strings?
    – benwiggy
    Mar 3 '17 at 19:30
  • I can confirm that this bug still exists in GPL Ghostscript 9.27 (2019-04-04) Sep 21 at 14:43
1

Think I've fixed it. It seems that GhostScript doesn't handle escape characters correctly, interpreting them literally. So you (I) need to santize the input by removing all backslashes from the filepath.

 filename=${filename//\\}

Again, very weird that such an ancient piece of software should have such a bug.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.