mirror of
https://github.com/alexanderepstein/Bash-Snippets
synced 2018-11-08 02:59:35 +01:00
83 lines
3.3 KiB
Bash
Executable File
83 lines
3.3 KiB
Bash
Executable File
#!/bin/bash
|
|
# Author: Alexander Epstein https://github.com/alexanderepstein
|
|
base=""
|
|
exchangeTo=""
|
|
|
|
## Grabs the base currency from the user and validates it with all the possible currency
|
|
## types available on the API
|
|
getBase()
|
|
{
|
|
echo -n "What is the base currency: "
|
|
read -r base
|
|
base=$(echo $base | tr /a-z/ /A-Z/)
|
|
if [[ $base != "AUD" && $base != "BGN" && $base != "BRL" \
|
|
&& $base != "CAD" && $base != "CHF" && $base != "CNY" && $base != "CZK" && $base != "DKK" \
|
|
&& $base != "EUR" && $base != "GBP" && $base != "HKD" && $base != "HRK" && $base != "HUF" \
|
|
&& $base != "IDR" && $base != "ILS" && $base != "INR" && $base != "JPY" && $base != "KRW" \
|
|
&& $base != "MXN" && $base != "MYR" && $base != "NOK" && $base != "NZD" && $base != "PHP" \
|
|
&& $base != "PLN" && $base != "RON" && $base != "RUB" && $base != "SEK" && $base != "SGD" \
|
|
&& $base != "THB" && $base != "TRY" && $base != "USD" && $base != "ZAR" ]];then
|
|
unset base
|
|
echo "Invalid base currency"
|
|
getBase
|
|
fi
|
|
}
|
|
|
|
## Grabs the exchange to currency from the user and validates it with all the possible currency
|
|
## types available on the API
|
|
getExchangeTo()
|
|
{
|
|
echo -n "What currency to exchange to: "
|
|
read -r exchangeTo
|
|
exchangeTo=$(echo $exchangeTo | tr /a-z/ /A-Z/)
|
|
if [[ $exchangeTo != "AUD" && $exchangeTo != "BGN" && $exchangeTo != "BRL" \
|
|
&& $exchangeTo != "CAD" && $exchangeTo != "CHF" && $exchangeTo != "CNY" && $exchangeTo != "CZK" && $exchangeTo != "DKK" \
|
|
&& $exchangeTo != "EUR" && $exchangeTo != "GBP" && $exchangeTo != "HKD" && $exchangeTo != "HRK" && $exchangeTo != "HUF" \
|
|
&& $exchangeTo != "IDR" && $exchangeTo != "ILS" && $exchangeTo != "INR" && $exchangeTo != "JPY" && $exchangeTo != "KRW" \
|
|
&& $exchangeTo != "MXN" && $exchangeTo != "MYR" && $exchangeTo != "NOK" && $exchangeTo != "NZD" && $exchangeTo != "PHP" \
|
|
&& $exchangeTo != "PLN" && $exchangeTo != "RON" && $exchangeTo != "RUB" && $exchangeTo != "SEK" && $exchangeTo != "SGD" \
|
|
&& $exchangeTo != "THB" && $exchangeTo != "TRY" && $exchangeTo != "USD" && $exchangeTo != "ZAR" ]];then
|
|
echo "Invalid exchange currency"
|
|
unset getExchangeTo
|
|
getExchangeTo
|
|
fi
|
|
}
|
|
|
|
## Get the amount that will be exchanged and validate that the user has entered a number (decimals are allowed)
|
|
getAmount()
|
|
{
|
|
echo -n "What is the amount being exchanged: "
|
|
read -r amount
|
|
if [[ ! "$amount" =~ ^[0-9]+(\.[0-9]+)?$ ]]
|
|
then
|
|
echo "The amount has to be a number"
|
|
unset amount
|
|
getAmount
|
|
fi
|
|
}
|
|
|
|
checkInternet()
|
|
{
|
|
echo -e "GET http://google.com HTTP/1.0\n\n" | nc google.com 80 > /dev/null 2>&1
|
|
if [ $? -eq 0 ]; then
|
|
return 0
|
|
else
|
|
echo "Error: no active internet connection" >&2
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
## Grabs the exchange rate and does the math for converting the currency
|
|
convertCurrency()
|
|
{
|
|
exchangeRate=$(curl -s "http://api.fixer.io/latest?base=$base&symbols=$exchangeTo" | grep -Eo "[0-9][.][0-9]*") > /dev/null
|
|
exchangeAmount=$(echo "$exchangeRate * $amount" | bc )
|
|
echo "$amount $base is equal to $exchangeAmount $exchangeTo"
|
|
}
|
|
|
|
checkInternet || exit 1 # check if we have a valid internet connection if this isnt true the rest of the script will not work so stop here
|
|
getBase # get base currency
|
|
getExchangeTo # get exchange to currency
|
|
getAmount # get the amount to be converted
|
|
convertCurrency # grab the exhange rate and perform the conversion
|