Skip to content

Instantly share code, notes, and snippets.

@Fraasi
Created October 26, 2019 15:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fraasi/e5697ee8e7132ea77f85f9e31a703fca to your computer and use it in GitHub Desktop.
Save Fraasi/e5697ee8e7132ea77f85f9e31a703fca to your computer and use it in GitHub Desktop.
Curl random quote and pass it to cowsay
#!/bin/bash
declare -a MODES
MODES=("b" "d" "g" "p" "s" "t" "w" "y")
declare -a COWS
COWS=("tableflip" "aperture-blank" "beavis.zen" "bees" "biohazard" "box" "broken-heart" "cat" "cat2" "clippy" "cowfee" "cube" "default" "dragon" "hedgehog" "kilroy" "kosh" "nyan" "owl" "psychiatrichelp" "psychiatrichelp2" "shrug" "squirrel" "tux")
MODES_LENGTH=${#MODES[@]}
MODES_INDEX=$(($RANDOM % $MODES_LENGTH))
RANDOM_MODE=${MODES[$MODES_INDEX]}
COWS_LENGTH=${#COWS[@]}
COWS_INDEX=$(($RANDOM % $COWS_LENGTH))
RANDOM_COW=${COWS[$COWS_INDEX]}
echo ${RANDOM_COW[@]} -${RANDOM_MODE[@]} Fetching quote...
URL="your quote api url"
HTTP_RESPONSE=$( curl -sw "%{http_code}" $URL )
HTTP_CODE=${HTTP_RESPONSE:${#HTTP_RESPONSE}-3}
if [ ! "$HTTP_CODE" -eq 200 ]; then
cowsay -f ${RANDOM_COW} -${RANDOM_MODE} -W 70 "$HTTP_CODE Failure!"
else
HTTP_BODY=${HTTP_RESPONSE:0:${#HTTP_RESPONSE}-3}
# parse body and pass it to cowsay
echo $HTTP_BODY | sed -e 's/[{""}]/''/g' | cowsay -f ${RANDOM_COW} -${RANDOM_MODE} -W 70
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment