US Dollar amount to Bitcoin Dollar amount

I found myself needing to calculate how much Bitcoin I’d get for a certain US dollar amount, and I grew tired of checking websites.  I made a quick and easy bash shell script that takes US dollar amount as parameter, and returns the current bitcoin value per $1 USD along with the amount of Bitcoin the amount of USD given in the parameter would purchase.  The output is as follows:

MBP:~ danlund$ ./scripts/btc_amount.sh 200
Current BTC Value per USD: 3796.03
Answer: 200 will currently buy 0.05268600 BTC

Without any further adieu, here’s the script.  Feel free to edit as you wish, I’m just looking to share it.  This will not unfortunately function without a working internet connection since it pulls current BTC data from Coinbase.

#!/bin/bash
if [[ -z $1 ]]; then
echo “Converts USD amount to BTC amount”
echo “$0 <USD amount to convert to BTC>”
exit 1
fi
DOLLARS=$1
AMOUNT=`curl -s https://api.coinbase.com/v2/prices/BTC-USD/spot | sed ‘s/,/\’$’\n/g’ | sed ‘s/{/\’$’\n/g’ | grep amount | sed ‘s/”//g’ | sed ‘s/:/\’$’ /g’ | awk ‘{ print $2 }’`
BTC_PER_DOLLAR=`bc <<< “scale=8;1/${AMOUNT}”`
BTC_QUANTITY=`bc <<< “scale=8;x=${DOLLARS} * ${BTC_PER_DOLLAR}; if(x<1) print 0; x”`
echo “Current BTC Value per USD: ${AMOUNT}”
echo “Answer: ${DOLLARS} will currently buy ${BTC_QUANTITY} BTC”

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s