[Scottish] Script hates whitespace

Claudio Calvelli scotlug at intercal.org.uk
Fri Jul 19 10:36:13 UTC 2013


Keith Wyse writes:
> Hi all;
> I'm having some trouble with a little script I'm writing - it hates
> whitespace.
> I have a bunch of books in .pdf format, but I can read them better in .epub
> because of page resizing and font resizing. I wanted to use Calibre's
> command line ebook-convert to modify them.
> So the main line of the script is;
> 
> find . -name "*.pdf" -execdir sh -c 'ebook-convert {} "`basename {}
> .pdf`.epub"' \;

That results in the command being expanded several times by various shells,
not a good combination.  You could try:

find . -name '*.pdf' -print0 | \
while read -r -d '' f; do pushd "`dirname "$f"`"; n="`basename "$f"`"; \
ebook-convert "$n" "${n%.pdf}.epub"; popd; done

which is naturally self-explanatory but in case it isn't, you have
your shell reading each name (NUL-delimited, using "-print0" to "find"
and "-d ''" to the shell's "read" builtin) and then use the shell's own
string manipulation to remove the ".pdf".  I don't have ebook-convert,
but if I replace it with "echo" I get the list I would expect.

Or you could use something other than a shell one-liner :-)

HTH
C




More information about the Scottish mailing list