Discussion:
Problems handling spaces in filenames
(too old to reply)
Philipp Klaus Krause
2019-01-26 20:36:28 UTC
Permalink
I rarely write bash scripts, so I am not surprised I ran into trouble
with spaces in filenames in one:

#!/bin/bash
for i in `seq $2 $3`; do
base=$1-$i
image=${base}.jpeg
pdflist="${pdflist} \"$base.pdf\""
tesseract "$image" "$base" -l deu pdf;
done
pdfjoin $pdflist && rm $pdflist
mv "$1"-"$3"-joined.pdf "$1".pdf

I wrote and use this script to convert JPEG images (names e.g.
test-1.jpeg, test-2-jpeg, etc) to searchable pdf (names test.pdf).
However, the

pdfjoin $pdflist

isn't working when $1 contains spaces. Apparently the quotes are
disappearing somewhere along the way.

Philipp
Joe Rosevear
2019-06-15 20:23:35 UTC
Permalink
Post by Philipp Klaus Krause
I rarely write bash scripts, so I am not surprised I ran into trouble
#!/bin/bash
for i in `seq $2 $3`; do
base=$1-$i
image=${base}.jpeg
pdflist="${pdflist} \"$base.pdf\""
tesseract "$image" "$base" -l deu pdf;
done
pdfjoin $pdflist && rm $pdflist
mv "$1"-"$3"-joined.pdf "$1".pdf
I wrote and use this script to convert JPEG images (names e.g.
test-1.jpeg, test-2-jpeg, etc) to searchable pdf (names test.pdf).
However, the
pdfjoin $pdflist
isn't working when $1 contains spaces. Apparently the quotes are
disappearing somewhere along the way.
Philipp
Maybe

pdfjoin "$pdflist" && rm "$pdflist"

-Joe
Stephane Chazelas
2019-10-22 21:14:30 UTC
Permalink
Post by Philipp Klaus Krause
I rarely write bash scripts, so I am not surprised I ran into trouble
#!/bin/bash
for i in `seq $2 $3`; do
base=$1-$i
image=${base}.jpeg
pdflist="${pdflist} \"$base.pdf\""
tesseract "$image" "$base" -l deu pdf;
done
pdfjoin $pdflist && rm $pdflist
mv "$1"-"$3"-joined.pdf "$1".pdf
I wrote and use this script to convert JPEG images (names e.g.
test-1.jpeg, test-2-jpeg, etc) to searchable pdf (names test.pdf).
However, the
pdfjoin $pdflist
isn't working when $1 contains spaces. Apparently the quotes are
disappearing somewhere along the way.
[...]

Use an array variable to store more than one value.

#! /bin/bash -
pdflist=()
for ((i = $2; i <= $3; i++)) {
base=$1-$i
image=$base.jpeg
pdflist+=("$base.pdf")
tesseract "$image" "$base" -l deu pdf
}
pdfjoin "${pdflist[@]}" && rm "${pdflist[@]}"
mv "$1-$3-joined.pdf" "$1.pdf"


Note that it won't work properly if $1 starts with "-". The
way to address it would be to use "--" for all commands, but
unfortunately it won't work with pdfjoin which calls pdfjam,
itself a Bourne-shell script that breaks when file names start
with "-" even if you pass "--" to pdfjoin.

One way to address it would be to prefix relative paths with
"./".

case $1 in
(/*) prefix=$1;;
(*) prefix=./$1;;
esac
--
Stephane
Continue reading on narkive:
Loading...