Skip to content

Instantly share code, notes, and snippets.

@joaorafaelm
Created November 6, 2014 21:10
Show Gist options
  • Save joaorafaelm/d68adb5ec5413ffc6d09 to your computer and use it in GitHub Desktop.
Save joaorafaelm/d68adb5ec5413ffc6d09 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# Aluno: João Rafael
#
# Pede o nome do arquivo
# parametro: @
function pedeArquivo {
echo -n "Digite o nome do arquivo: "
read arquivo
existeArquivo $arquivo
}
# Verifica se o arquivo existe e testa a abertura e fechamento de cheques.
# parametro: @arquivo [Nome do arquivo]
function existeArquivo {
ARQUIVO="./arquivos/$1"
if [ -f $ARQUIVO ]; then
# verifica de o cheque tem duas ocorrencias de CHEQUES
if [ $(egrep '^CHEQUES;' $ARQUIVO | wc -l) -gt 1 ]; then
#prossegue com o código e verifica se tem abertura (A) e fechamento (F)
if [[ $(egrep '^CHEQUES;[AF]' $ARQUIVO | wc -l) -gt 1 ]]; then
output $ARQUIVO
else
goto_error "Indicativo de início ou término de arquivo inválido!"
fi
else
goto_error "Erro na identificação do arquivo!"
fi
else
goto_error "Arquivo não encontrado!"
fi
}
# Exibe mensagem $1 e chama a função @pedeArquivo novamento.
# parametro: @1 [Mensagem de erro]
function goto_error {
echo $1
pedeArquivo #recursivo
}
# Echo descrição para cada linha do arquivo.
# parametro: @1 [Caminho do arquivo]
function output {
for linha in $(cat $1)
do
#echo $linha | awk '/^CHEQUES;A/{print $0 " -> Início do cheque - "}'
echo $linha | awk '/^LOTE;A/{print $0 " -> LOTE DE INICIO - "}'
echo $linha | awk '/^[0-9]/{print $0 " -> CHEQUE - "}'
echo $linha | egrep '^LOTE;F' | verificaLote $ARQUIVO
#echo $linha | awk '/^CHEQUES;F/{print $0 " -> Fechamento do cheque - "}'
done
}
# Verifica somatório de cada lote e echo LOTE (F).
# parametro: $@ [buffer do pipe]
function verificaLote {
read pipe
if [[ $( echo $pipe | grep '^L' | wc -l) -eq 1 ]]; then
numLote=$(echo $pipe | cut -d ";" -f3)
loteTotal=$(echo $pipe | cut -d ";" -f4)
batch="$(cat $ARQUIVO)"
# pega o que ta entre o lote
batch=${batch##*"LOTE;A;$numLote"}
batch=${batch%%"$pipe"*}
if [[ $( echo $batch | egrep '^[1-9]' | wc -l) -eq 1 ]]; then
total=0
for valor in $batch
do
valor=$(echo $valor | cut -d ";" -f2)
total=$(echo $total + $valor | bc)
done
if [[ $loteTotal == $total ]]; then
echo "$pipe -> LOTE DE FIM - ** FECHAMENTO OK **"
else
echo "$pipe -> LOTE DE FIM - ** FECHAMENTO INCORRETO **"
fi
else
echo "$pipe -> LOTE DE FIM -> Número do lote de inicio diferente do fechamento do lote - ** FECHAMENTO INCORRETO **"
fi
fi
}
pedeArquivo #inicia script
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment