Skip to content

Instantly share code, notes, and snippets.

@nerun
Last active January 28, 2026 16:43
Show Gist options
  • Select an option

  • Save nerun/515c5e77ad7ebc1b5b7fa8df6ced2a8b to your computer and use it in GitHub Desktop.

Select an option

Save nerun/515c5e77ad7ebc1b5b7fa8df6ced2a8b to your computer and use it in GitHub Desktop.
Imprime índices econômicos direto no terminal. Compatibilidade POSIX: testado em sh, bash e zsh.
#!/bin/bash
#
# indices.sh - version 1.7 - 2026-01-28
# Copyright (c) 2024, 2025, 2026 Daniel Dias Rodrigues <[email protected]>.
# No rights reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the Creative Commons Zero 1.0 Universal (CC0 1.0) Public
# Domain Dedication (https://creativecommons.org/publicdomain/zero/1.0/).
#
URL_DOLAR="https://www.exchange-rates.org/pt/historico/usd-brl"
URL_EURO="https://www.exchange-rates.org/pt/historico/eur-brl"
URL_OURO="https://pt.bullion-rates.com/gold/BRL-history.htm"
URL_BTC="https://api.binance.com/api/v3/ticker/price?symbol=BTCBRL"
URL_DOLAR_REALTIME="https://www.xe.com/currencyconverter/convert/?Amount=1&From=USD&To=BRL"
URL_EURO_REALTIME="https://www.xe.com/currencyconverter/convert/?Amount=1&From=EUR&To=BRL"
_validContent(){
local CONTENT="$(wget -qO- $1)"
local CURRENT=""
local COUNT=0
while [ -z "$CURRENT" ]; do
COUNT=$(($COUNT + 1))
DATE=$(date -d "$(date +%F) -$COUNT day" +%x) # 27/01/2026 (ontem)
CURRENT=$(echo "$CONTENT" | grep "$DATE" -A3 | tail -n 1)
done
COIN=$(echo "$CURRENT" | sed -e 's/.*">//g' -e 's/ BRL.*//g')
echo "$DATE $COIN"
}
USD=$(_validContent "$URL_DOLAR")
USD_DATE=$(echo "$USD" | cut -d' ' -f1)
USD_COIN=$(echo "$USD" | cut -d' ' -f2)
EURO=$(_validContent "$URL_EURO")
EURO_DATE=$(echo "$EURO" | cut -d' ' -f1)
EURO_COIN=$(echo "$EURO" | cut -d' ' -f2)
GOLD_CONTENT="$(wget -qO- $URL_OURO)"
GOLD_CURRENT="$(echo "$GOLD_CONTENT" | grep "$(date +%d/%m/%y)" -A2)"
while [ -z "$GOLD_CURRENT" ]; do
COUNT=$(($COUNT + 1))
GOLD_DATE=$(date --date="$(date +%F) -$COUNT day" +%d/%m/%y)
GOLD_CURRENT=$(echo "$GOLD_CONTENT" | grep "$GOLD_DATE" -A2)
done
if [ -z "$GOLD_DATE" ]; then
GOLD_DATE=$(date +%x)
else
GOLD_DATE=$(echo "$GOLD_DATE" | sed -r 's/(.{6})/\120/')
fi
GOLD_clear=$(echo "$GOLD_CURRENT" | tail -2 | sed -e 's/ //g' \
-e 's/<.*\">//g' -e 's/<.*//g')
GOLDg=$(echo "$GOLD_clear" | head -1)
GOLDoz=$(echo "$GOLD_clear" | tail -1)
# Current values
btc_section=$(wget -qO- "$URL_BTC" | sed -r 's/[[:alpha:]:",\{\}]//g' | \
awk '{printf "%.2f\n", $1}')
btc=$(echo "$btc_section" | \
awk '{printf "%\047d,%s\n", $1, substr($1, length($1)-1)}')
_validRealtime(){
local RATE=$(wget -qO- "$1" | grep -oP "1 $2 =.{8}" | \
sed -e "s/1 $2 = //" -e 's/\./,/')
echo "$RATE"
}
usd=$(_validRealtime "$URL_DOLAR_REALTIME" "USD")
eur=$(_validRealtime "$URL_EURO_REALTIME" "EUR")
echo "Valores históricos
==================
Dólar¹ | Euro¹ | Ouro²
(${USD_DATE}) | (${EURO_DATE}) | (${GOLD_DATE})
R$ ${USD_COIN} | R$ ${EURO_COIN} | R$ ${GOLDg}/g
| R$ ${GOLDoz},00/oz
Valores correntes
=================
1,00 USD³ = R$ ${usd}
1,00 EUR³ = R$ ${eur}
1,00 BTC⁴ = R$ ${btc}
Fontes:
¹ www.exchange-rates.org
² pt.bullion-rates.com
³ xe.com
⁴ api.binance.com"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment