Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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