Skip to content

Instantly share code, notes, and snippets.

@franklinbr
Created May 14, 2019 18:08
Show Gist options
  • Save franklinbr/d3caf1917776ad9a3513b6fa8e286987 to your computer and use it in GitHub Desktop.
Save franklinbr/d3caf1917776ad9a3513b6fa8e286987 to your computer and use it in GitHub Desktop.
#!/bin/bash
arquivo_entrada='script.sql'
arquivo_tmp='temporario.sql'
arquivo_saida='saida.sql'
function AjustesIniciais() {
head -n1 ${arquivo_entrada} | tail -n1 > ${arquivo_tmp}
echo "" >> ${arquivo_tmp}
head -n3 ${arquivo_entrada} | tail -n1 | sed 's/\$/variavel/g' >> ${arquivo_tmp}
}
function AplicaValores() {
local query=$(head -n3 ${arquivo_tmp} | tail -n1)
local parametro_linha=$(head -n1 ${arquivo_tmp} | tail -n1)
local parametro=$(echo $parametro_linha | tr "," "\n" | cut -d= -f2 | sed 's/ //')
IFS='
'
local incrementoj=0
local vetor=()
for argumento in $parametro
do
vetor[$incrementoj]=$argumento
let incrementoj++
done
local incrementoj=1
local query_cache=""
for i in ${vetor[@]}
do
query=$(echo $query | sed "s/\<variavel${incrementoj}\>/$i/g")
query_cache=$query
let incrementoj++
done
echo $query >> ${arquivo_saida}
# adiciona ; no final de cada linha
sed -i 's/$/;/g' ${arquivo_saida}
# deleta arquivo temporario
if [ -f "$arquivo_tmp" ]
then
rm ${arquivo_tmp}
fi
}
AjustesIniciais
AplicaValores
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment