From 46720e05e31b0e28977168d93641694794768977 Mon Sep 17 00:00:00 2001 From: Maxim Likhachev Date: Sat, 22 Aug 2020 11:43:42 +0300 Subject: [PATCH] ++currency --- README.md | 4 ++++ currency | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100755 currency diff --git a/README.md b/README.md index 07a3f9d..c50a1ba 100644 --- a/README.md +++ b/README.md @@ -113,3 +113,7 @@ will create the symbolic link for each script in the \ directory. - [x-open](x-open) opens files via xdg-open (on Linux systems) or via open command (on MacOS). - [xstab](xstab) runs [st](https://st.suckless.org/) terminal with the [tabbed](https://tools.suckless.org/tabbed/). +## Other + +- [currency](currency) shows the currency rate. + diff --git a/currency b/currency new file mode 100755 index 0000000..3d33846 --- /dev/null +++ b/currency @@ -0,0 +1,38 @@ +#!/bin/bash + +# +# Simple currency converter +# + +SHORT_OUTPUT=false + +exchange() { + local api_url="https://api.exchangeratesapi.io/latest?base=${1^^}&symbols=${2^^}" + + curl -s -q -L "$api_url" \ + | jq -r --arg short "$SHORT_OUTPUT" ' + def round: + .*100.0 + 0.5 | floor / 100.0; + + if ($short == "true") then + .rates | to_entries | .[] | (.value | round | tostring) + else + "1 " + .base + " ≈ " + + (.rates | to_entries | .[] | (.value | round | tostring) + " " + .key) + end + ' +} + +if [ $# -le 1 ]; then + echo "USAGE: $(basename "$0") ?-s? FROM TO1,TO2,...,TOn" + echo " e.g: $(basename "$0") ?-s? USD EUR,RUB" + echo "Set -s to show only value." +else + if [ "$1" == "-s" ]; then + SHORT_OUTPUT=true + shift + fi + + exchange "$@" +fi +