marvvin

IRC bot
Log | Files | Refs | README | LICENSE

commit cfa162bd26a0730393ca5d8a993bd8a92804d5ba
parent 06185ff2f7c2c1e5c62120e78e786a103812b22e
Author: hhvn <dev@hhvn.uk>
Date:   Wed, 13 Apr 2022 18:02:47 +0100

Improve bin/currency error handling

Diffstat:
Mbin/currency | 11+++++++----
1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/bin/currency b/bin/currency @@ -1,11 +1,14 @@ #!/bin/sh -[ -z $1 ] && echo "usage: crypto <amount> <input currency> in <output currency>" && exit +[ -z $1 ] && echo "usage: crypto <amount> <input currency> in <output currency>" && exit 1 +amount="$1" +[ "$(echo "$amount" | tr -d '\n')" != "$(echo "$amount" | tr -dc 0-9)" ] && echo "not a number" && exit 1 from="$2" -to="$4" +[ -z $4 ] && to="$3" || to="$4" mult=$(curl -qs "https://min-api.cryptocompare.com/data/price?fsym=$from&tsyms=$to" | awk -F"[\"}{:]" '{print $5}') +[ -z $mult ] && echo "invalid currencies" && exit 1 -echo "scale=15; $1 * $mult" | bc | tr -d '\n' -echo " $4" +echo "scale=15; $amount * $mult" | bc | tr -d '\n' +echo " $to" | tr '[:lower:]' '[:upper:]'