BASH Script to Query and Monitor the Crypto Prices (Exchange Rate to Fiat)



BASH Script to Query and Monitor the Crypto Prices (Exchange Rate to Fiat) | ninjasquad

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/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]
#!/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:

1
2
3
4
5
6
7
8
9
10
11
12
$ ./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"
$ ./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"
ticker-crypto-bash BASH Script to Query and Monitor the Crypto Prices (Exchange Rate to Fiat) bash script blockchain Cryptocurrency

Simple BASH Script to Query the Crypto Prices

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/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
#!/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:

1
2
# every 60 seconds
$ watch -n 60 ./coin BTC ETH STEEM TRX
# every 60 seconds
$ watch -n 60 ./coin BTC ETH STEEM TRX
monitor-crypto-prices BASH Script to Query and Monitor the Crypto Prices (Exchange Rate to Fiat) bash script blockchain Cryptocurrency

Monitor the Crypto Prices in BASH (via monitor)

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

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading…

571 words
Last Post: Tron Blockchain: Javascript (NodeJs) Function to Get the Frozen Balance (Staked TRX) for Energy and Bandwidth





Source: Internet

Leave a Comment

We are offering free coding tuts

X