Adds better command options. Few bug fixes. Better syntax.

This commit is contained in:
Llewellyn van der Merwe 2022-01-04 12:20:09 +02:00
parent 8db4e91296
commit bf9df3ef6b
Signed by: Llewellyn
GPG Key ID: EFC0C720A240551C
2 changed files with 111 additions and 24 deletions

View File

@ -9,25 +9,69 @@ command -v curl >/dev/null 2>&1 || {
echo >&2 "We require curl for this script to run, but it's not installed. Aborting." echo >&2 "We require curl for this script to run, but it's not installed. Aborting."
exit 1 exit 1
} }
# set the query (default: 1 John 3:16-18) # set the query (default: 1 John 3:16-18)
QUERY="${1:-62 3:16-18}" SCRIPTURE="${1:-62 3:16-18}"
# set the translation (default: kjv) # set the translation (default: kjv)
VERSION="${2:-kjv}" : "${VERSION:=kjv}"
# check if we have options
while :; do
case $1 in
-s | --scripture) # Takes an option argument; ensure it has been specified.
if [ "$2" ]; then
SCRIPTURE=$2
shift
else
echo >&2 '"--scripture" requires a non-empty option argument.'
exit 17
fi
;;
-s=?* | --scripture=?*)
SCRIPTURE=${1#*=} # Delete everything up to "=" and assign the remainder.
;;
-s= | --scripture=) # Handle the case of an empty --scripture=
echo >&2 '"--scripture=" requires a non-empty option argument.'
exit 17
;;
-v | --version) # Takes an option argument; ensure it has been specified.
if [ "$2" ]; then
VERSION=$2
shift
else
echo >&2 '"--version" requires a non-empty option argument.'
exit 17
fi
;;
-v=?* | --version=?*)
VERSION=${1#*=} # Delete everything up to "=" and assign the remainder.
;;
-v= | --version=) # Handle the case of an empty --version=
echo >&2 '"--version=" requires a non-empty option argument.'
exit 17
;;
*) # Default case: No more options, so break out of the loop.
break ;;
esac
shift
done
# get the name from the query TODO: find better filter # get the name from the query TODO: find better filter
BOOKNAME=$(echo ${QUERY%%[0-9][0-9]?:*} | xargs echo -n) BOOK_NAME=$(echo "${SCRIPTURE%%[0-9][0-9]?:*}" | xargs echo -n)
BOOKNAME=$(echo ${BOOKNAME%%[0-9]?:*} | xargs echo -n) BOOK_NAME=$(echo "${BOOK_NAME%%[0-9]?:*}" | xargs echo -n)
BOOKNAME=$(echo ${BOOKNAME%%[0-9]:*} | xargs echo -n) BOOK_NAME=$(echo "${BOOK_NAME%%[0-9]:*}" | xargs echo -n)
# check if the name was given by number # check if the name was given by number
re='^[0-9]+$' re='^[0-9]+$'
if [[ "$BOOKNAME" =~ $re ]]; then if [[ "$BOOK_NAME" =~ $re ]]; then
BOOK_NR="${BOOKNAME}" BOOK_NR="${BOOK_NAME}"
else else
# get the list of books from the API to get the book number # get the list of books from the API to get the book number
BOOKS=$(curl -s "https://getbible.net/v2/${VERSION}/books.json") BOOKS=$(curl -s "https://getbible.net/v2/${VERSION}/books.json")
BOOK_NR=$(echo "$BOOKS" | jq -r ".[] | select(.name == \"${BOOKNAME}\") | .nr") BOOK_NR=$(echo "$BOOKS" | jq -r ".[] | select(.name == \"${BOOK_NAME}\") | .nr")
fi fi
# get chapter and verses numbers # get chapter and verses numbers
NUMBERS=$(echo "${QUERY/$BOOKNAME/}" | xargs echo -n) NUMBERS=$(echo "${SCRIPTURE/$BOOK_NAME/}" | xargs echo -n)
# get chapter number # get chapter number
CHAPTER_NR=$(echo "${NUMBERS%:*}" | xargs echo -n) CHAPTER_NR=$(echo "${NUMBERS%:*}" | xargs echo -n)
# get verses numbers # get verses numbers
@ -37,7 +81,7 @@ CHAPTER=$(curl -s "https://getbible.net/v2/${VERSION}/${BOOK_NR}/${CHAPTER_NR}.j
# read verses into array by comma separator # read verses into array by comma separator
IFS=',' read -ra VERSES_NR_ARRAY <<<"${VERSES_NR}" IFS=',' read -ra VERSES_NR_ARRAY <<<"${VERSES_NR}"
# start the scripture array # start the scripture array
SCRIPTURE=() SCRIPTURE_VERSE=()
# load the verses in this loop over the verse numbers sets # load the verses in this loop over the verse numbers sets
for VER_NR in "${VERSES_NR_ARRAY[@]}"; do for VER_NR in "${VERSES_NR_ARRAY[@]}"; do
# check if this is a range of verses # check if this is a range of verses
@ -48,14 +92,14 @@ for VER_NR in "${VERSES_NR_ARRAY[@]}"; do
while [ "$START" -le "$END" ]; do while [ "$START" -le "$END" ]; do
VERSE=$(echo "$CHAPTER" | jq -r ".verses[] | select(.verse == ${START})") VERSE=$(echo "$CHAPTER" | jq -r ".verses[] | select(.verse == ${START})")
TEXT=$(echo "$VERSE" | jq -r '.text') TEXT=$(echo "$VERSE" | jq -r '.text')
SCRIPTURE+=("${START} ${TEXT}") SCRIPTURE_VERSE+=("${START} ${TEXT}")
START=$(($START + 1)) START=$(( START + 1 ))
done done
else else
VERSE=$(echo "$CHAPTER" | jq ".verses[] | select(.verse == ${VER_NR})") VERSE=$(echo "$CHAPTER" | jq ".verses[] | select(.verse == ${VER_NR})")
TEXT=$(echo "$VERSE" | jq -r '.text') TEXT=$(echo "$VERSE" | jq -r '.text')
SCRIPTURE+=("${VER_NR} ${TEXT}") SCRIPTURE_VERSE+=("${VER_NR} ${TEXT}")
fi fi
done done
# we return the scripture one verse per/line # we return the scripture one verse per/line
IFS=$'\n'; echo "${SCRIPTURE[*]}" IFS=$'\n'; echo "${SCRIPTURE_VERSE[*]}"

View File

@ -10,25 +10,68 @@ command -v curl >/dev/null 2>&1 || {
exit 1 exit 1
} }
# set the query (default: 1 John 3:16-18) # set the query (default: 1 John 3:16-18)
QUERY="${1:-62 3:16-18}" SCRIPTURE="${1:-62 3:16-18}"
# set the translation (default: kjv) # set the translation (default: kjv)
VERSION="${2:-kjv}" : "${VERSION:=kjv}"
# check if we have options
while :; do
case $1 in
-s | --scripture) # Takes an option argument; ensure it has been specified.
if [ "$2" ]; then
SCRIPTURE=$2
shift
else
echo >&2 '"--scripture" requires a non-empty option argument.'
exit 17
fi
;;
-s=?* | --scripture=?*)
SCRIPTURE=${1#*=} # Delete everything up to "=" and assign the remainder.
;;
-s= | --scripture=) # Handle the case of an empty --scripture=
echo >&2 '"--scripture=" requires a non-empty option argument.'
exit 17
;;
-v | --version) # Takes an option argument; ensure it has been specified.
if [ "$2" ]; then
VERSION=$2
shift
else
echo >&2 '"--version" requires a non-empty option argument.'
exit 17
fi
;;
-v=?* | --version=?*)
VERSION=${1#*=} # Delete everything up to "=" and assign the remainder.
;;
-v= | --version=) # Handle the case of an empty --version=
echo >&2 '"--version=" requires a non-empty option argument.'
exit 17
;;
*) # Default case: No more options, so break out of the loop.
break ;;
esac
shift
done
# get the name from the query TODO: find better filter # get the name from the query TODO: find better filter
BOOKNAME=$(echo ${QUERY%%[0-9][0-9]?:*} | xargs echo -n) BOOK_NAME=$(echo "${SCRIPTURE%%[0-9][0-9]?:*}" | xargs echo -n)
BOOKNAME=$(echo ${BOOKNAME%%[0-9]?:*} | xargs echo -n) BOOK_NAME=$(echo "${BOOK_NAME%%[0-9]?:*}" | xargs echo -n)
BOOKNAME=$(echo ${BOOKNAME%%[0-9]:*} | xargs echo -n) BOOK_NAME=$(echo "${BOOK_NAME%%[0-9]:*}" | xargs echo -n)
# check if the name was given by number # check if the name was given by number
re='^[0-9]+$' re='^[0-9]+$'
if [[ "$BOOKNAME" =~ $re ]]; then if [[ "$BOOK_NAME" =~ $re ]]; then
# get the list of books from the API to get the book number # get the list of books from the API to get the book number
BOOKS=$(curl -s "https://getbible.net/v2/${VERSION}/books.json") BOOKS=$(curl -s "https://getbible.net/v2/${VERSION}/books.json")
BOOK_NAME=$(echo "$BOOKS" | jq -r ".[] | select(.nr == ${BOOKNAME}) | .name") BOOK_FULL_NAME=$(echo "$BOOKS" | jq -r ".[] | select(.nr == ${BOOK_NAME}) | .name")
# get chapter and verses numbers # get chapter and verses numbers
NUMBERS=$(echo "${QUERY/$BOOKNAME/}" | xargs echo -n) NUMBERS=$(echo "${SCRIPTURE/$BOOK_NAME/}" | xargs echo -n)
# get chapter and verses numbers # get chapter and verses numbers
echo "${BOOK_NAME} ${NUMBERS}" echo "${BOOK_FULL_NAME} ${NUMBERS}"
else else
echo "$QUERY" echo "$SCRIPTURE"
fi fi
exit 0 exit 0