mirror of
https://github.com/alexanderepstein/Bash-Snippets
synced 2018-11-08 02:59:35 +01:00
Currency can now take in arguments
This commit is contained in:
parent
3c54484761
commit
7ef1637098
@ -5,7 +5,7 @@ base=""
|
|||||||
exchangeTo=""
|
exchangeTo=""
|
||||||
currentVersion="1.1.0"
|
currentVersion="1.1.0"
|
||||||
## Grabs the base currency from the user and validates it with all the possible currency
|
## Grabs the base currency from the user and validates it with all the possible currency
|
||||||
## types available on the API
|
## types available on the API and guides user through input (doesnt take in arguments)
|
||||||
getBase()
|
getBase()
|
||||||
{
|
{
|
||||||
echo -n "What is the base currency: "
|
echo -n "What is the base currency: "
|
||||||
@ -23,9 +23,27 @@ getBase()
|
|||||||
getBase
|
getBase
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
## Checks base currency from the user and validates it with all the possible currency
|
||||||
|
## types available on the API (requires argument)
|
||||||
|
checkBase()
|
||||||
|
{
|
||||||
|
base=$1
|
||||||
|
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"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
## Grabs the exchange to currency from the user and validates it with all the possible currency
|
## Grabs the exchange to currency from the user and validates it with all the possible currency
|
||||||
## types available on the API
|
## types available on the API and guides user through input (doesnt take in arguments)
|
||||||
getExchangeTo()
|
getExchangeTo()
|
||||||
{
|
{
|
||||||
echo -n "What currency to exchange to: "
|
echo -n "What currency to exchange to: "
|
||||||
@ -44,7 +62,27 @@ getExchangeTo()
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
## Grabs the exchange to currency from the user and validates it with all the possible currency
|
||||||
|
## types available on the API (requires arguments)
|
||||||
|
checkExchangeTo()
|
||||||
|
{
|
||||||
|
exchangeTo=$1
|
||||||
|
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
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
## Get the amount that will be exchanged and validate that the user has entered a number (decimals are allowed)
|
## Get the amount that will be exchanged and validate that the user has entered a number (decimals are allowed)
|
||||||
|
## doesnt take in argument, it guides user through input
|
||||||
getAmount()
|
getAmount()
|
||||||
{
|
{
|
||||||
echo -n "What is the amount being exchanged: "
|
echo -n "What is the amount being exchanged: "
|
||||||
@ -57,6 +95,19 @@ getAmount()
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
## Get the amount that will be exchanged
|
||||||
|
## validate that the user has entered a number (decimals are allowed and requires argument)
|
||||||
|
checkAmount()
|
||||||
|
{
|
||||||
|
amount=$1
|
||||||
|
if [[ ! "$amount" =~ ^[0-9]+(\.[0-9]+)?$ ]]
|
||||||
|
then
|
||||||
|
echo "The amount has to be a number"
|
||||||
|
unset amount
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
checkInternet()
|
checkInternet()
|
||||||
{
|
{
|
||||||
echo -e "GET http://google.com HTTP/1.0\n\n" | nc google.com 80 > /dev/null 2>&1
|
echo -e "GET http://google.com HTTP/1.0\n\n" | nc google.com 80 > /dev/null 2>&1
|
||||||
@ -154,6 +205,17 @@ if [[ $# == 0 ]]; then
|
|||||||
getExchangeTo # get exchange to currency
|
getExchangeTo # get exchange to currency
|
||||||
getAmount # get the amount to be converted
|
getAmount # get the amount to be converted
|
||||||
convertCurrency # grab the exhange rate and perform the conversion
|
convertCurrency # grab the exhange rate and perform the conversion
|
||||||
elif [[ $1 == "update" ]]; then
|
exit 0
|
||||||
|
elif [[ $# == "1" ]]; then
|
||||||
|
if [[ $1 == "update" ]];then
|
||||||
update
|
update
|
||||||
|
else
|
||||||
|
echo "Not a valid argument"
|
||||||
|
fi
|
||||||
|
elif [[ $# == 3 ]];then
|
||||||
|
checkBase $1
|
||||||
|
checkExchangeTo $2
|
||||||
|
checkAmount $3
|
||||||
|
convertCurrency
|
||||||
|
exit 0
|
||||||
fi
|
fi
|
||||||
|
Loading…
Reference in New Issue
Block a user