Skip to content

Instantly share code, notes, and snippets.

@donpdonp

donpdonp/coin.sh

Last active Aug 11, 2020
Embed
What would you like to do?
coincap.io coin price lookup
#!/bin/bash
if [ -z "$1" ];
then
COINS=`cat ~/.coins`
PARAM="ids=$COINS"
else
echo $1
PARAM="search=$1"
fi
DATA=`curl --silent "https://api.coincap.io/v2/assets?$PARAM"`
echo "$DATA" | jq -r 'def leftpad(str; len): str + (" " * (len - (str|length)));
def numpad(str; dec; frac): (str|index(".")) as $dot | " " * (dec-$dot) + str[0:$dot+frac+1];
def longest(path): map(path|length) | max;
def biggest(path): map(path | index(".")) | max;
.data | longest(.symbol) as $sym_max |
longest(.id) as $id_max |
biggest(.priceUsd) as $price_max |
.[] |
[leftpad(.symbol; $sym_max),
leftpad(.id; $id_max),
"$" + numpad(.priceUsd; $price_max; 5)
]
| join (" ")'
$ cat ~/.coins
bitcoin,ethereum
$ coin
bitcoin,ethereum
BTC bitcoin $11540.53330
ETH ethereum $ 386.07154
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.