How to define a multiple page ranges for pdftk with a bash variable
0
votes
2
answers
222
views
I'm using Arch linux, Openbox window manager, and bash.
Everything is up to date with the latest versions.
Can anyone tell me why I can't get the
"$page_range"
variable to show up within pdftk
when I specify a couple of page ranges as 3-5 7-9
?
When I specify only one page range 3-5
in my yad pop-up box everything works as it should.
pdftk does allow more than one page range to be defined within the command. Indeed when I type the command out on the command line without using bash variables within it, pdftk works as expected taking the page ranges 3-5 7-9
. Just not when I contain this value within the variable "$page_range"
.
All I want to do is extract page ranges 3-5 and 7-9 from file
/home/$USER/my_file.pdf
into another pdf file
using the variable $page_range
to define my ranges.
Here is my simple script.
#!/bin/bash
# collect the values with yad
extract_values=$(yad --form --width=200 \
--title="Enter the page ranges you wish to extract" \
--text="\n\n Enter the page ranges you wish to extract\n as eg 301-302\n or 301-302 305-306\n for grouping" \
--field="Page range":text "11-13 21-23" \
--button="Cancel!gtk-close":2 \
--button="Edit script":1 \
--button="Submit":0)
# strip out the values from the string
page_range=$(echo $extract_values | cut -d '|' -f 1)
echo $page_range
# produce a unique file extender
page_range_slugify="$(echo "$page_range" | sed 's/ /_/g')"
echo;echo $page_range_slugify
echo
# specify the filename
f=/home/$USER/my_file.pdf
# get path and file name without pdf extension
fz="${f%.*}"
# check everything is as it should be
yad --text="\n page range = $page_range\n page_range_slugify = $page_range_slugify\n file + path without file extension = $fz\n\n"
# below works only for one range but will not expand for two page ranges
pdftk "$f" cat "$page_range" output "$fz"_"$page_range_slugify".pdf
# below takes one range only as above
#pdftk "$f" cat "$(printf %s "$page_range")" output "$fz"_"$page_range_slugify".pdf
# below takes both ranges when ranges are directly placed within the command
#pdftk "$f" cat 3-5 7-9 output "$fz"_"$page_range_slugify".pdf
Asked by Kes
(909 rep)
Sep 13, 2023, 10:06 AM
Last activity: Sep 15, 2023, 09:14 AM
Last activity: Sep 15, 2023, 09:14 AM