~/bin mkdir
How to Create a Bash Script to Extract Bibliographic References from DOIs
Dealing with references can feel like a real headache. However, recently I came across a fantastic solution by Richard Mclearn. This blog post is all about making your life easier with a simple tutorial for Mac users. I’ll show you how to set up a getref
function that quickly grabs bibliographic information in BibTeX format from a DOI or a DOI URL.
Step-by-Step Guide
Step 1: Prepare Your Workspace
First, we need to create a dedicated folder to store your getref
function. This not only helps in organizing your scripts but also in managing your PATH environment efficiently.
You can create the folder following this steps:
1.1. Open the terminal (cmd+space then type terminal).
1.2. Create a new folder named bin in your home directory:
type
and press return in your keyboard.
I created the bin folder in my home directory. It will make it easier to access the script later on.
Step 2: Move to the bin Folder
Navigate to the newly created folder by typing
~/bin. cd
Step 3: Create the Script File
Create a new file named getref by typing
touch getref
This file will be our script.
Step 4: Add the Script Code
Open the getref
file in your favorite text editor. For simplicity, you can use Nano.
nano getref
In the inside of the file, copy and paste the following code snippet. This script checks if a DOI or DOI URL is provided as an argument and then fetches the bibliographic information in BibTeX format.
#!/bin/bash
# Check if an argument is provided
if [ "$#" -ne 1 ]; then
"Usage: $0 <DOI or DOI URL>"
echo 1
exit
fi
# Determine if the input is a DOI URL or just a DOI number
if [[ "$1" =~ ^https:// ]]; then
# Extract the DOI number from the URL
=$(echo "$1" | sed 's|https://doi.org/||')
DOIelse
# Assume the input is just a DOI number
="$1"
DOI
fi
# Extract and print the reference
-LH "Accept: application/x-bibtex" "https://doi.org/$DOI" curl
Step 5: Make the Script Executable
Ensure the script can be executed by running in the terminal
+x ~/bin/getref chmod
Notice that I used the path to my file, which is located in the bin folder in my home directory.
Step 6: Update Your Shell Configuration
Now, Open your shell configuration file (.zshrc for Zsh or .bashrc for Bash) using a text editor, e.g.,
~/.zshrc. nano
You will need to add ~/bin to your PATH. To do this, add the following line at the end of the file:
="$HOME/bin:$PATH". export PATH
Save and close the file.
Step 7: Apply the Changes
Apply the changes to your current session by sourcing your configuration file:
~/.zshrc source
for Zsh
or
~/.bashrc source
for Bash.
How to Use Your New Script
To use your script, simply type getref
in your termina followed by a DOI or a DOI URL. For example:
10.1080/09687637.2023.2167649 getref
or
://doi.org/10.1080/09687637.2023.2167649 getref https
The script will fetch and display the bibliographic information in BibTeX format.
Done! This is a handy tool that simplifies the process of fetching bibliographic data. I hope you enjoy it. If you have any questions or suggestions, feel free to leave a comment below.