dotfiles

configuration files for shell, text editor, graphical environment, etc.
git clone git://src.adamsgaard.dk/dotfiles # fast
git clone https://src.adamsgaard.dk/dotfiles.git # slow
Log | Files | Refs | README | LICENSE Back to index

currency (1016B)


      1 #!/bin/sh
      2 set -e
      3 
      4 if [ $# -lt 1 ]; then
      5     echo "Usage: $0 [amount] [currency1] [currency2]"
      6     echo
      7     echo "If currency1 and currency2 and are not defined they default to USD and DKK, respectively."
      8     echo "Currency strings are not case sensitive. The amount defaults to 1."
      9     echo
     10     echo "Example 1: $0 USD GBP"
     11     echo "Example 2: $0 42"
     12     echo "Example 3: $0 14 GBP EUR"
     13     exit 0
     14 fi
     15 
     16 if [ -n "$1" ]; then a=$1; else a=1; fi
     17 if [ -n "$2" ]; then from=$(echo "$2" | awk '{print toupper($0)}'); else from=USD; fi
     18 if [ -n "$3" ]; then to=$(echo "$3" | awk '{print toupper($0)}'); else to=DKK; fi
     19 if [ "$to" = "$from" ]; then echo "Nothing to do!"; exit 2; fi
     20 
     21 url="https://free.currencyconverterapi.com/api/v6/convert?q=${from}_${to}&compact=ultra&apiKey=$(pass Online/currencyconverterapi.com-apikey)"
     22 result="$(curl "$url" 2>/dev/null | sed 's/.*://;s/}$//')"
     23 
     24 if [ -n "$result" ]; then
     25 	echo "$a $from = $(echo "${result}*${a}" | bc -l) $to"
     26 else
     27 	echo "error: could not get conversion"
     28 	exit 1
     29 fi