Skip to content

Instantly share code, notes, and snippets.

@cristianmiranda
Last active April 27, 2023 13:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cristianmiranda/6d8ce52d5f2eb2b9c1f8bc4aa9217fcc to your computer and use it in GitHub Desktop.
Save cristianmiranda/6d8ce52d5f2eb2b9c1f8bc4aa9217fcc to your computer and use it in GitHub Desktop.
dolar.sh
#!/usr/bin/env bash
# Function to output usage instructions
function usage {
echo "Usage: $(basename $0) <options>" >&2
echo
echo "Gets the current USD value" >&2
echo
echo "OPTIONS:" >&2
echo " --blue | -b : Dólar Blue" >&2
echo " --oficial | -o : Dólar Oficial" >&2
}
function fetchUSDValue {
output=`curl -s https://mercados.ambito.com/dolar/$1/variacion`
buyPrice=`echo $output | jq '.compra' | sed 's/"//g'`
sellPrice=`echo $output | jq '.venta' | sed 's/"//g'`
variation=`echo $output | jq '.variacion' | sed 's/"//g'`
lastClosingValue=`echo $output | jq '.valor_cierre_ant' | sed 's/"//g'`
if [[ $variation == *-* ]]; then
variation="🔴 $variation"
else
variation="🟢 $variation"
fi
echo "💵 $buyPrice / $sellPrice // $variation ($lastClosingValue)"
}
# Check arguments
if [ "$1" == "-h" -o "$1" == "--help" ]; then
usage
exit 0
elif [ $# -eq 0 ]; then
echo "No file specified; reading from stdin" >&2
exec "$0" -
fi
# Check curl is available
type curl &>/dev/null || {
echo "Couldn't find curl, which is required." >&2
exit 17
}
# Loop through arguments
while [ $# -gt 0 ]; do
arg="$1"
shift
if [ "$arg" == "-b" -o "$arg" == "--blue" ]; then
fetchUSDValue informal
fi
if [ "$arg" == "-o" -o "$arg" == "--oficial" ]; then
fetchUSDValue oficial
fi
done
@matiascova10
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment