3

How can I make this script cleaner? I'm assuming one way is to create something that holds the various paths and loops over those. Is there anything else I can do to make this cleaner and not just a repeated block of loops?

#!/bin/bash/
SRCD='/Users/Connor/Documents/GitHub/WhatThePDF/dat/forms/*.pdf'
for file in $SRCD
do
    echo ${file}
    destfile="${file/.pdf/_source.txt}"
    echo ${destfile}
    qpdf --qdf --object-streams=disable ${file} ${destfile}
done

SRCD='/Users/Connor/Documents/GitHub/WhatThePDF/dat/data/*.pdf'
for file in $SRCD
do
    echo ${file}
    destfile="${file/.pdf/_source.txt}"
    echo ${destfile}
    qpdf --qdf --object-streams=disable ${file} ${destfile}
done

SRCD='/Users/Connor/Documents/GitHub/WhatThePDF/dat/maps/*.pdf'
for file in $SRCD
do
    echo ${file}
    destfile="${file/.pdf/_source.txt}"
    echo ${destfile}
    qpdf --qdf --object-streams=disable ${file} ${destfile}
done

SRCD='/Users/Connor/Documents/GitHub/WhatThePDF/dat/none/*.pdf'
for file in $SRCD
do
    echo ${file}
    destfile="${file/.pdf/_source.txt}"
    echo ${destfile}
    qpdf --qdf --object-streams=disable ${file} ${destfile}
done
Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
  • 2
    You can wrap the inner loop into an outer for loop over the various SRCD values. You can extract the common code into a function. Also, using arrays for lists of files (and quoting the variables) would make it work for paths containing spaces, too. – choroba Sep 01 '16 at 15:43

1 Answers1

4

We can simplify this to a single loop, just by passing all the filenames in a single go:

#!/bin/bash

for file in /Users/Connor/Documents/GitHub/WhatThePDF/dat/{forms,data,maps,none}/*.pdf
do
  echo "${file}"
  destfile="${file/.pdf/_source.txt}"
  echo "${destfile}"
  qpdf --qdf --object-streams=disable "${file}" "${destfile}"
done

For readability we might split this out a little

eg cd to the directory and abort if it fails:

#!/bin/bash

cd /Users/Connor/Documents/GitHub/WhatThePDF/dat || exit 255

for file in {forms,data,maps,none}/*.pdf
do
  echo "${file}"
  destfile="${file/.pdf/_source.txt}"
  echo "${destfile}"
  qpdf --qdf --object-streams=disable "${file}" "${destfile}"
done