Prezados,<br><br> esta é minha primeira mensagem para esta lista.<br><br> Começo com uma questão interessante. Fiz o script abaixo para obter em formato CSV valores de alguns fundos de investimento. Gostaria de anexar este comportamento à funcionalidade de busca automática de cotações, mas não sei por onde começar. Alguém palpita?<br>
<br><font size="1"><span style="font-family: courier new,monospace;">#!/bin/bash</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"># Copyright (C) Leonardo F. Cardoso (não experiente com shell script)</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">#####################################################</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">CAMINHO=/home/leonardo/Leo</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">DATA_FINAL=$(date +%d/%m/%Y)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">DATA_FINAL_TXT=$(date +%d-%m-%Y)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">DATA_INICIAL=$(date --date="5 days ago" +%d/%m/%Y)</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">wget -o $CAMINHO/wgetFundos.log -O $CAMINHO/fundos-$DATA_FINAL_TXT.html --post-data="data_inicio=$DATA_INICIAL&data_fim=$DATA_FINAL&fundos_ok='123242'" <a href="http://www.anbid.com.br/prodserv/recuperar_fim.asp">http://www.anbid.com.br/prodserv/recuperar_fim.asp</a></span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"># Até agora, obtivemos os dados ainda em formato HTML, vamos obter as cotas mais recentes</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">html2text $CAMINHO/fundos-$DATA_FINAL_TXT.html | while read LINHA_LIDA</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">do</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> if [[ $LINHA_LIDA == "Fundo: "* ]]</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> then</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> NOME_FUNDO=$(echo $LINHA_LIDA | gawk '{ match ($0, /Fundo: .* Tipo: /, arr); print arr[0]}' | gawk '{sub (/Fundo: /, ""); sub (/ Tipo:/,""); print }')</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> # Guardamos o nome do fundo atualmente em processamento em NOME_FUNDO</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> # echo "DEBUG" $NOME_FUNDO</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> fi</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> # Prossegue lendo linhas até encontrar uma linha com o separador</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> # Separador = "================================"*</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> if [[ $LINHA_LIDA == "================================"* ]]</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> then</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> DATA_FUNDO=$(echo $LINHA_ANTERIOR | gawk 'BEGIN {FS="|"} { print $2 }' | gawk '{sub (/_/, ""); print}') </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> # echo "DEBUG" $DATA_FUNDO</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> VALOR_FUNDO=$(echo $LINHA_ANTERIOR | gawk 'BEGIN {FS="|"} { print $4 }' | gawk '{sub (/_/, ""); print}') </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> # echo "DEBUG" $VALOR_FUNDO</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> echo $DATA_FUNDO";"$VALOR_FUNDO";"$NOME_FUNDO </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> fi</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> # Guarda a linha anterior pois no momento em que houver o separador, a cota estará nela</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> LINHA_ANTERIOR=$LINHA_LIDA</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">done</span><br style="font-family: courier new,monospace;">
</font><br><br> Sds<br> Leonardo<br><br clear="all">-----------------------------------------<br>Leonardo F. Cardoso<br>leonardo.f.cardoso "at" <a href="http://gmail.com" target="_blank">gmail.com</a><br>leofcar "at" <a href="http://ig.com.br" target="_blank">ig.com.br</a> (Alternativo)<br>
MSN: <a href="mailto:lfcardoso@hotmail.com" target="_blank">lfcardoso@hotmail.com</a><br>-----------------------------------------<br>