Last active
February 11, 2025 19:53
-
-
Save beporter/0f7b23ad032a1871e71654a27a306516 to your computer and use it in GitHub Desktop.
Quick bash script to generate a gravatar.com URL for a provided email address. Drop in your PATH and `chmod u+x`.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Generate the correct Gravatar URL for the provided email address. | |
# When the -o option is provided, `open` the URL in your default browser. | |
usage() { >&2 echo "Usage: ${0##*/} [-o] <email>"; exit 1; } | |
GRAVATAR_BASE_URL="https://gravatar.com/avatar/" | |
GRAVATAR_OPTIONS="d=wavatar" | |
# Parse command line arguments. | |
while getopts "oh" OPT; do | |
case "$OPT" in | |
o) OPEN_URL=true ;; | |
*) usage ;; | |
esac | |
done | |
shift $((OPTIND - 1)) | |
# Ensure the email address is provided. | |
if [ -z "$1" ]; then | |
>&2 echo "Error: A single email address is required." | |
usage | |
fi | |
# Generate the Gravatar URL. | |
EMAIL_HASH=$(echo -n "$1" | tr '[:upper:]' '[:lower:]' | sha256) | |
GRAVATAR_URL="${GRAVATAR_BASE_URL}${EMAIL_HASH}?${GRAVATAR_OPTIONS}" | |
# Output the Gravatar URL. | |
echo "$GRAVATAR_URL" | |
# If the -o flag is set, open the URL in the default browser. | |
if [ "$OPEN_URL" = true ]; then | |
open "$GRAVATAR_URL" | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash DON'T USE THIS VERSION! ONLY FOR COMPARISON. | |
# Prompt provided to ChatGPT 4.0: | |
# Please produce a bash shell script that accepts an optional "-o" argument and a required email address as command line arguments, and outputs the correct gravatar URL for the provided email address. When the "-o" option is present, also use the MacOS "open" command to open the URL in the user's default browser. | |
# Errors: | |
# - `/bin/bash` isn't guaranteed to be present on all versions of all typical operating systems. `/usr/bin/env bash` tends to be more broadly reliable because it'll inherit the current user's full $PATH. | |
# - The script flat out fails without an initial `-o` option, because it assumes the email address is always in `$2`. | |
# - According to [the Gravatar docs](https://docs.gravatar.com/api/avatars/hash/), the email should be hashed using SHA256, not MD5. (Although it does appear URLs employing md5 still function.) | |
# - The supplied "acceptable options" to `getopts` is `'o'`, even though there is a catch-all `*` case for displaying usage. This means getopts outputs a warning about an unrecognized option even for `gravatar.sh -h` formulation. | |
# - The whole `get_gravatar_url` function would be good abstraction in a full fledged program, but for a legitimately simple bash script it's needless misdirection. (Compare to my above version.) | |
# - Usage output is NOT well abstracted into a reusable function, nor does it print to STDERR. | |
# - The usage text also typically truncates `$0` to just the basename via `${0##*/}` instead of revealing the whole filesystem path to the executable. | |
# --- | |
# Function to calculate the MD5 hash of the email address | |
get_gravatar_url() { | |
local email=$1 | |
local hash=$(echo -n "$email" | tr '[:upper:]' '[:lower:]' | md5) | |
echo "https://www.gravatar.com/avatar/$hash" | |
} | |
# Check if no email address is provided | |
if [ -z "$2" ]; then | |
echo "Usage: $0 [-o] <email>" | |
exit 1 | |
fi | |
# Parse options | |
OPEN=false | |
while getopts "o" opt; do | |
case $opt in | |
o) | |
OPEN=true | |
;; | |
*) | |
echo "Usage: $0 [-o] <email>" | |
exit 1 | |
;; | |
esac | |
done | |
# Email is the last argument after options | |
EMAIL="${@:$OPTIND:1}" | |
# Get the Gravatar URL for the provided email | |
GRAVATAR_URL=$(get_gravatar_url "$EMAIL") | |
echo "Gravatar URL: $GRAVATAR_URL" | |
# If -o option is set, open in browser | |
if [ "$OPEN" = true ]; then | |
open "$GRAVATAR_URL" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment