I am trying to create a function that I can pass a query into and loop through items items in chunks.
scale=10
loops=10
function runQuery (){
loop=1
lower=1
upper=scale
while [ $loop -le $loops ]; do
echo "Loop -> $loop between $lower and $upper"
mysql -NB -h 127.0.0.1 -e "$query"
let loop=$loop+1
let lower=lower+scale
let upper=upper+scale
done
}
query='delete from x where id between $lower and $upper'
runQuery
In the example above the variables are set to an empty string when the query is passed in rather than $lower
and $upper
being used within the function. I am guessing what I need to do is pass in a placeholder for the variable which will be defined within the function... The variables are being correctly substituted in in the echo statement but are not in the query
. I could easily just put the query in the function, but I want to be able run this on multiple tables and send in different queries without having to duplicate all the looping variable set up throughout the script.
I have done quite a bit of searching for this but not sure I am searching for the right things...
BASH VERSION 3.2