-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetPdfPages.sh
More file actions
executable file
·58 lines (50 loc) · 1.46 KB
/
getPdfPages.sh
File metadata and controls
executable file
·58 lines (50 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env bash
# script used to feed numbers into pdftk based on section name of notes
#TODO: (multiple) auto complete in .bashrc
toc="Table Of Contents | Page i" # Ignore page numbers for TOC
# args: pattern filename output
# eg: ./getPdfPages.sh 6.5: math123Notes.pdf math123_NoteKeys/math123Notes_6p5.pdf
pattern=$1
fname=$2
output=$3
#TODO: make help message
#https://www.howtogeek.com/beginner-friendly-ways-to-level-up-your-bash-scripts/
_help() {
cat <<EOF
Usage: $(basename "$0") [OPTIONS] \"pattern\" \"fname\" \"output\"
OPTIONS
-h display this help menu
EOF
}
while getopts "hd" opt; do
case "$opt" in
h)
_help
exit 0
;;
d)
set -x #enable debug
;;
esac
done
#TODO remove flags from position variables
# Print matching page range
pages=$(comm -23 --nocheck-order \
<(pdfgrep --page-number --no-filename --cache "$pattern" $fname | cut -d: -f1 | sort --numeric --unique) \
<(pdfgrep --page-number --no-filename --cache "$toc" $fname | cut -d: -f1 | sort --unique ))
# check if $pages is empty
if [ ! -z "$pages" ]; then
# Check if $output undefined
if [ ! -n "$output" ]; then
#TODO: Autocomplete for filenames
echo $(echo "$pages" | head -n1)-$(echo "$pages" | tail -n1)
read -p "Specify output file: " output
fi
# Check if $output defined now
if [ -n "$output" ]; then
# echo output: $output
pdftk $fname cat $pages output $output
fi
else
echo "Empty page range!"
fi