-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathdoi2bibtex
More file actions
executable file
·65 lines (54 loc) · 1.36 KB
/
doi2bibtex
File metadata and controls
executable file
·65 lines (54 loc) · 1.36 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
59
60
61
62
63
64
65
#!/bin/bash
#
# Get a BibTeX citation from a DOI
set -e
set -o pipefail
usage()
{
echo "usage : doi2bibtex [ -d DOI ] [ -f FILE ]"
echo ""
echo "A simple Bash script to take a single DOI or a list of DOIs from a file"
echo "and use cURL to fetch the BibTeX citation for the given DOI(s)."
echo ""
echo "If no DOI or file is provided the adjacent README.md is parsed for DOIs"
echo "using cat/grep/awk. Note that the URL for the DOI MUST be the last"
echo "element of the row and separated by a space from the previous word."
}
while getopts "d:f:?h" option
do
case $option in
d)
DOI=${OPTARG};;
f)
FILE=${OPTARG};;
h|?)
usage
exit 0;;
esac
done
DOI_FILE=()
if [ "${FILE}" ]; then
while IFS= read -r LINE; do
DOI_FILE+=("${LINE}")
done < "${FILE}"
fi
if [ "${DOI}" ]; then
DOI_FILE+=("${DOI}")
fi
if [ -p /dev/sdtin ]; then
while IFS= read -r LINE; do
DOI_FILE+=("${LINE}")
done
fi
if [ -z "${FILE}" ] && [ -z "${DOI}" ]; then
mapfile -t DOI_FILE < <(grep "^\*" README.md | grep "doi" | awk '{print $NF}')
fi
_fetch()
{
DOI=${1}
# Pipe through grep so that only valid references are output, we capture non-matches (when grep returns 1)
curl --silent -LH "Accept: text/bibliography; style=bibtex" ${DOI} | { grep "^ @" || test $? = 1; }
}
for doi in "${DOI_FILE[@]}"; do
_fetch ${doi}
done