I'm trying to write a bash scrip that uses Imagemagick to convert PDF's to images in a given directory (passed as an argument), and then save that conversion into a sub-folder of that directory. I have a script the works perfectly fine for converting and saving into the same directory, but when I try to add the path I'd like to converted files to be saved at the script breaks.
Here's what I have so far that works:
DIR="$@"
EXT="*.pdf"
mkdir -p "${DIR}images"
shopt -s nullglob
for pdffile in "$DIR"*.pdf; do
convert -density 300 -depth 8 -quality 90 -trim "${pdffile}" "${pdffile%.*}".jpeg
done
Here's what I've tried for saving into a sub-folder. If I add "images/..." however to the last argument in converted like so:
DIR="$@"
EXT="*.pdf"
mkdir -p "${DIR}images"
shopt -s nullglob
for pdffile in "$DIR"*.pdf; do
convert -density 300 -depth 8 -quality 90 -trim "${pdffile}" "images/${pdffile%.*}".jpeg
done
This is what happens when I execute the script:
$ ls foo
bar.pdf baz.pdf biz.pdf
$ ./pdf-to-img.sh foo/
convert-im6.q16: unable to open image `images/foo/bar.jpeg': No such file or directory @ error/blob.c/OpenBlob/2701.
convert-im6.q16: unable to open image `images/foo/baz.jpeg': No such file or directory @ error/blob.c/OpenBlob/2701.
convert-im6.q16: unable to open image `images/foo/biz.jpeg': No such file or directory @ error/blob.c/OpenBlob/2701.
What am I doing wrong?
${pdffile%.*}will include theDIRprefix. Also please edit your question to show exactly how you're running the script - and the complete (unedited) error.DIRcomponent (in this casefoo) from${pdffile%.*}"mogrifywith-path