Simple BASH Script to Monitor Cryptos

in crypto •  2 years ago  (edited)

With the Crypto Price Lookup API and the JSON jq tool, we can make a simple bash script to query crypto prices such as bitcoins, TRON, steem:

#!/bin/bash
# filename: ticker

check_json=$(echo "{ }" | jq)
if [ "$check_json" != "{}" ]; then
        echo "jq not installed"
        exit 1
fi

if [ "$1" == "" ]; then
        echo "e.g. ./$0 steem usd"
fi

API="https://price.justyy.workers.dev/query/"

curl -s -X POST -d "$*" $API | jq .result[0]

Example usages:

$ ./ticker btc
"1 BTC = 15657.051999999998 USD"
$ ./ticker eth
"1 ETH = 1107.6736666666666 USD"
$ ./ticker btc gbp
"1 btc = 13765.275 gbp"
$ ./ticker 2 btc eur
"2 btc = 31593.86 eur"
$ ./ticker 2 btc eth
"2 btc = 28.362718608445565 eth"
$ ./ticker btc 2 eth
"0.141076979417348 btc = 2 eth"

The BASH sends a simple API request and parse the JSON response via the jq which will need to be installed via apt install jq.

We can monitor a command by using the watch command (parameter -n gives the interval to run the command), and we have the following tailored script so that we can query multiple coins re multiple fiat currencies at the same time:

#!/bin/bash
# filename: coin

check_json=$(echo "{ }" | jq)
if [ "$check_json" != "{}" ]; then
        echo "jq not installed"
        exit 1
fi

if [ "$1" == "" ]; then
        echo "e.g $0 btc"
        exit 2
fi

API="https://price.justyy.workers.dev/query/"
for i in $*; do
        for j in USD GBP EUR CNY; do
                ticker=$(curl -s -X POST -d "$i $j" $API | jq .result[0] | awk '{printf "%.3f", $4;}')
                echo "1 $i = $ticker $j"
        done
        echo -------------------------
done

Example usage:

# every 60 seconds
$ watch -n 60 ./coin BTC ETH STEEM TRX

Note that the Cryptos are volatile and do know how to manage your risks!

--EOF (The Ultimate Computing & Technology Blog) --


Reposted to Blog

Steem to the Moon!

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE BLURT!