Reputation: 100
I'm trying to use the variable $y in this snippet below but keep running into the filename showing up as $y.pdf
ls -v *.jpg | tr '\n' ' ' | sed 's/$/\ $y.pdf/' | xargs convert
I've also tried the below:
ls -v *.jpg | tr '\n' ' ' | sed 's/$/\'"$y.pdf"'/' | xargs convert
ls -v *.jpg | tr '\n' ' ' | sed 's/$/\ {$y}.pdf/' | xargs convert
The top one usually fails, as sed is expecting something else. The bottom one just makes my filename {$y}.pdf.
Any ideas for me?
Thanks!
Upvotes: 0
Views: 53
Reputation: 532418
Never mind the quoting problem; this is the wrong approach entirely. Just use a for
loop to avoid all kinds of potential problems.
for f in *.jpg; do
convert "$f" "${f%.jpg}.pdf"
done
${f%.jpg}
expands to the name of the current file minus the .jpg
extension.
To merge the files into one PDF is even simpler (I think):
convert *.jpg "$y.pdf"
Assuming ls -v
outputs the file names in the correct order, and there aren't any of the usual concerns with parsing ls
involved, use
ls -v *.jpg > input.txt
convert @input.txt "$y.pdf"
(There might be a way to avoid the use of a tempfile, maybe as simple as ls -v *.jpg | convert @- "$y.pdf"
, but I'm too lazy to figure out all the ways convert
can be called.)
Upvotes: 1