2

I'm trying to split tempfile.pdf with pdftk according to strings that I found on certain pagenumbers. Those pagenumbers are in tempfile.

n=0
prefix1="startpoint"
prefix2="stoppoint"
while read p; do
  echo $p > temp
  `cat temp` = prefix1$n
  echo $((p-1)) > temp
  `cat temp` = prefix2$n
  n=$((n+1));
done < tempfile

pdftk tempfile.pdf cat $startpoint0-$stoppoint1 output $1/01-0.pdf
pdftk tempfile.pdf cat $startpoint1-$stoppoint2 output $1/02-0.pdf
pdftk tempfile.pdf cat $startpoint2-end output $1/03-0.pdf

For some reason I get errors like this:

/usr/local/bin/skapa_digital_akt: rad 16: 1: kommandot finns inte
/usr/local/bin/skapa_digital_akt: rad 18: 0: kommandot finns inte
/usr/local/bin/skapa_digital_akt: rad 16: 6: kommandot finns inte
/usr/local/bin/skapa_digital_akt: rad 18: 5: kommandot finns inte
/usr/local/bin/skapa_digital_akt: rad 16: 13: kommandot finns inte
/usr/local/bin/skapa_digital_akt: rad 18: 12: kommandot finns inte
Error: Unexpected range end; expected a page
   number or legal keyword, here: 
   Exiting.
Errors encountered.  No output created.
Done.  Input errors, so no output created.
Error: Unexpected range end; expected a page
   number or legal keyword, here: 
   Exiting.
Errors encountered.  No output created.
Done.  Input errors, so no output created.
Error: Input page numbers include 0 (zero)
   The first PDF page is 1 (one)
   Exiting.
Errors encountered.  No output created.
Done.  Input errors, so no output created.

Obviously the variables startpoint/stoppoint are set wrongly. Anyone could help me out?

Many thanks!

/Paul

Paul Bergström
  • 253
  • 1
  • 2
  • 14

2 Answers2

2

What is

`cat temp` = prefix1$n

Supposed to do? You should know: it reads a command line from temp and executes it.

if temp contains a line with 1 (and n is == 0), then the executed command will be:

1 = prefix10

You can often help yourself debugging scripts by inserting set -x on top of the script or calling it using sh -x yourscript.sh

Daniel Alder
  • 5,031
  • 2
  • 45
  • 55
0

This worked:

n=0
while read p; do
  echo $p > tempA$n
  echo $((p-1)) > tempB$n
  n=$((n+1));
done < tempfile

startpoint0=$(cat tempA0)
stoppoint1=$(cat tempB1)
startpoint1=$(cat tempA1)
stoppoint2=$(cat tempB2)
startpoint2=$(cat tempA2)


pdftk tempfile.pdf cat $startpoint0-$stoppoint1 output $1/01-0.pdf
pdftk tempfile.pdf cat $startpoint1-$stoppoint2 output $1/02-0.pdf
pdftk tempfile.pdf cat $startpoint2-end output $1/03-0.pdf
Paul Bergström
  • 253
  • 1
  • 2
  • 14