Skip to content

Instantly share code, notes, and snippets.

@putnamhill
Created November 19, 2017 15:56
Show Gist options
  • Save putnamhill/3c3e08baca6d6a6d66b06c2a89c02480 to your computer and use it in GitHub Desktop.
Save putnamhill/3c3e08baca6d6a6d66b06c2a89c02480 to your computer and use it in GitHub Desktop.
Reads the coinmarketcap ticker and writes the prices for a cryptocoins to a file
#!/bin/bash
get_data() {
# if the parameter is a local file just cat it (for testing) otherwise try curl
{ test -r "$1" && cat "$1" || curl -s "$1"; } | \
jq --raw-output \
'.[] | "\(.symbol | ascii_downcase) \(.price_usd)"'
}
process() {
read -r filename price <<< $(get_data "$1")
printf "\$filename: %s\t\$price: %.2f\n" "$filename" "$price"
#echo "$price" > "$filename"
}
if [ ${#} -gt 0 ]; then # read urls from args on the command line if there are any
while [ ${#} -gt 0 ]; do
url="$1"
shift
process "$url"
done
else # read urls from stdin
while read -r url; do
process "$url"
done
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment